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();
 }
Beispiel #3
0
 private static function setModelClassMappings()
 {
     if (static::$modelClassMapping === null) {
         static::$modelClassMapping = array();
         foreach (static::getModelClasses() as $modelClassName) {
             $entity = $modelClassName::$entityType;
             $bundle = $modelClassName::$bundle;
             if (empty($entity)) {
                 throw new InvalidTypeException('Entity Type not defined for ' . $modelClassName);
             }
             $key = Model::getKeyForEntityAndBundle($entity, $bundle);
             static::$modelClassMapping[$key] = $modelClassName;
         }
     }
 }