public static function handle($entity, $trigger)
 {
     if (!empty($entity) && !empty($trigger)) {
         $entityType = $entity->getEntityTypeId();
         $bundle = $entity->bundle();
         if (Model::hasModelClassForEntityAndBundle($entityType, $bundle)) {
             $modelClass = Model::getModelClassForEntityAndBundle($entityType, $bundle);
             $model = new $modelClass($entity);
             switch ($trigger) {
                 case 'presave':
                     if ($model->entity->isNew()) {
                         $model->beforeInsert();
                     } else {
                         $model->beforeUpdate();
                     }
                     break;
                 case 'insert':
                     $model->afterInsert();
                     break;
                 case 'update':
                     $model->afterUpdate();
                     break;
                 case 'delete':
                     $model->beforeDelete();
                     break;
             }
         }
     }
 }
 public function setRelationshipMetaData()
 {
     // First we will get the field Definition to read our meta data from
     $relationshipSource = $this->relationshipSource;
     $fieldDefinition = $relationshipSource::getFieldDefinition($this->getField());
     if (empty($fieldDefinition)) {
         throw new \Drupal\spectrum\Exceptions\InvalidFieldException('Field ' . $this->getField() . ' not found on modeltype: ' . $relationshipSource);
     }
     $fieldSettings = $fieldDefinition->getItemDefinition()->getSettings();
     // Here we decide if our relationship is polymorphic or for a single entity/bundle type
     $relationshipEntityType = $fieldSettings['target_type'];
     $relationshipBundle = null;
     if (!empty($fieldSettings['handler_settings']['target_bundles'])) {
         // with all entity references this shouldn't be a problem, however, in case of 'user', this is blank
         // luckally we handle this correctly in getModelClassForEntityAndBundle
         $relationshipBundle = reset($fieldSettings['handler_settings']['target_bundles']);
     }
     $this->firstModelType = Model::getModelClassForEntityAndBundle($relationshipEntityType, $relationshipBundle);
     if (isset($fieldSettings['handler_settings']['target_bundles']) && sizeof($fieldSettings['handler_settings']['target_bundles']) > 1) {
         $this->isPolymorphic = true;
         $this->modelType = null;
     } else {
         $this->modelType = $this->firstModelType;
     }
     // Next we set the cardinality of the field, either we have a single reference or multiple references (single parent / multiple parents)
     $this->fieldCardinality = $fieldDefinition->getFieldStorageDefinition()->getCardinality();
 }
Example #3
0
 public function fetch($relationshipName)
 {
     $lastRelationshipNameIndex = strrpos($relationshipName, '.');
     if (empty($lastRelationshipNameIndex)) {
         $relationship = static::getRelationship($relationshipName);
         $relationshipQuery = $relationship->getRelationshipQuery();
         $relationshipCondition = $relationship->getCondition();
         if ($relationship instanceof FieldRelationship) {
             $fieldId = $this->getFieldId($relationship);
             if (!empty($fieldId)) {
                 // we start of by checking for multiple or single values allowed
                 // in case of a single, we'll just put a single Model
                 // else we'll put a collection of models
                 if (is_array($fieldId)) {
                     $relationshipCondition->value = $fieldId;
                     $relationshipCondition->operator = 'IN';
                     $relationshipQuery->addCondition($relationshipCondition);
                     $referencedEntities = $relationshipQuery->fetch();
                     if (!empty($referencedEntities)) {
                         $referencedModelType = null;
                         $referencedRelationship = null;
                         // the inverse relationship
                         foreach ($referencedEntities as $referencedEntity) {
                             $referencedModel = null;
                             if ($relationship->isPolymorphic || empty($referencedModelType)) {
                                 // if the relationship is polymorphic we can get multiple bundles, so we must define the modeltype based on the bundle and entity of the current looping entity
                                 // or if the related modeltype isn't set yet, we must set it once
                                 $referencedEntityType = $referencedEntity->getEntityTypeId();
                                 $referencedEntityBundle = $referencedEntity->type->target_id;
                                 $referencedModelType = Model::getModelClassForEntityAndBundle($referencedEntityType, $referencedEntityBundle);
                                 // we must also find the inverse relationship to put the current model on
                                 $referencedRelationship = $referencedModelType::getReferencedRelationshipForFieldRelationship($relationship);
                             }
                             // now that we have a model, lets put them one by one
                             $referencedModel = $referencedModelType::forge($referencedEntity);
                             $this->put($relationship, $referencedModel, true);
                             // And finally if we found an inverse relationship, lets put (this) on the inverse (defining an inverse is optional, so we can just as easily find no inverses)
                             if (!empty($referencedRelationship)) {
                                 $referencedModel->put($referencedRelationship, $this, true);
                             }
                         }
                     }
                 } else {
                     $relationshipCondition->value = $fieldId;
                     $relationshipCondition->operator = '=';
                     $relationshipQuery->addCondition($relationshipCondition);
                     $referencedEntity = $relationshipQuery->fetchSingle();
                     if (!empty($referencedEntity)) {
                         // if the relationship is polymorphic we can get multiple bundles, so we must define the modeltype based on the bundle and entity of the fetched entity
                         $referencedEntityType = $referencedEntity->getEntityTypeId();
                         $referencedEntityBundle = $referencedEntity->type->target_id;
                         $referencedModelType = Model::getModelClassForEntityAndBundle($referencedEntityType, $referencedEntityBundle);
                         // now that we have a model, lets put them one by one
                         $referencedModel = $referencedModelType::forge($referencedEntity);
                         $this->put($relationship, $referencedModel, true);
                         // we must also find the inverse relationship to put the current model on
                         $referencedRelationship = $referencedModelType::getReferencedRelationshipForFieldRelationship($relationship);
                         // And finally if we found an inverse relationship, lets put (this) on the inverse (defining an inverse is optional, so we can just as easily find no inverses)
                         if (!empty($referencedRelationship)) {
                             $referencedModel->put($referencedRelationship, $this, true);
                         }
                     }
                 }
             }
         } else {
             if ($relationship instanceof ReferencedRelationship) {
                 $id = $this->getId();
                 if (!empty($id)) {
                     $relationshipCondition->value = array($id);
                     $relationshipQuery->addCondition($relationshipCondition);
                     $referencingEntities = $relationshipQuery->fetch();
                     if (!empty($referencingEntities)) {
                         $referencingModelType = null;
                         foreach ($referencingEntities as $referencingEntity) {
                             $referencingModel = null;
                             if (empty($referencingModelType)) {
                                 // if the referencing modeltype isn't set yet, we must set it once
                                 $referencingEntityType = $referencingEntity->getEntityTypeId();
                                 $referencingEntityBundle = $referencingEntity->type->target_id;
                                 $referencingModelType = Model::getModelClassForEntityAndBundle($referencingEntityType, $referencingEntityBundle);
                             }
                             // now that we have a model, lets put them one by one
                             $referencingModel = $referencingModelType::forge($referencingEntity);
                             $this->put($relationship, $referencingModel, true);
                             $referencingModel->put($relationship->fieldRelationship, $this, true);
                         }
                     }
                 }
             }
         }
     } else {
         $secondToLastRelationshipName = substr($relationshipName, 0, $lastRelationshipNameIndex);
         $resultCollection = $this->get($secondToLastRelationshipName);
         $lastRelationshipName = substr($relationshipName, $lastRelationshipNameIndex + 1);
         $resultCollection->fetch($lastRelationshipName);
     }
 }