bundleKey = $this->entityType->getKey('bundle'); } /** * {@inheritdoc} */ public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { return new static( $entity_type ); } /** * {@inheritdoc} */ protected function doCreate(array $values) { // We have to determine the bundle first. $bundle = FALSE; if ($this->bundleKey) { if (!isset($values[$this->bundleKey])) { throw new EntityStorageException(String::format('Missing bundle for entity type @type', array('@type' => $this->entityTypeId))); } $bundle = $values[$this->bundleKey]; } $entity = new $this->entityClass(array(), $this->entityTypeId, $bundle); foreach ($entity as $name => $field) { if (isset($values[$name])) { $entity->$name = $values[$name]; } elseif (!array_key_exists($name, $values)) { $entity->get($name)->applyDefaultValue(); } unset($values[$name]); } // Set any passed values for non-defined fields also. foreach ($values as $name => $value) { $entity->$name = $value; } return $entity; } /** * {@inheritdoc} */ public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $storage_definition) { } /** * {@inheritdoc} */ public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) { } /** * {@inheritdoc} */ public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $storage_definition) { } /** * {@inheritdoc} */ public function onFieldDefinitionCreate(FieldDefinitionInterface $field_definition) { } /** * {@inheritdoc} */ public function onFieldDefinitionUpdate(FieldDefinitionInterface $field_definition, FieldDefinitionInterface $original) { } /** * {@inheritdoc} */ public function onFieldDefinitionDelete(FieldDefinitionInterface $field_definition) { } /** * {@inheritdoc} */ public function onBundleCreate($bundle) { } /** * {@inheritdoc} */ public function onBundleRename($bundle, $bundle_new) { } /** * {@inheritdoc} */ public function onBundleDelete($bundle) { } /** * {@inheritdoc} */ public function purgeFieldData(FieldDefinitionInterface $field_definition, $batch_size) { $items_by_entity = $this->readFieldItemsToPurge($field_definition, $batch_size); foreach ($items_by_entity as $items) { $items->delete(); $this->purgeFieldItems($items->getEntity(), $field_definition); } return count($items_by_entity); } /** * Reads values to be purged for a single field. * * This method is called during field data purge, on fields for which * onFieldDelete() or onFieldDelete() has previously run. * * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition * The field definition. * @param $batch_size * The maximum number of field data records to purge before returning. * * @return \Drupal\Core\Field\FieldItemListInterface[] * An array of field item lists, keyed by entity revision id. */ abstract protected function readFieldItemsToPurge(FieldDefinitionInterface $field_definition, $batch_size); /** * Removes field items from storage per entity during purge. * * @param ContentEntityInterface $entity * The entity revision, whose values are being purged. * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition * The field whose values are bing purged. */ abstract protected function purgeFieldItems(ContentEntityInterface $entity, FieldDefinitionInterface $field_definition); /** * {@inheritdoc} */ public function finalizePurge(FieldStorageDefinitionInterface $storage_definition) { } /** * Checks translation statuses and invoke the related hooks if needed. * * @param \Drupal\Core\Entity\ContentEntityInterface $entity * The entity being saved. */ protected function invokeTranslationHooks(ContentEntityInterface $entity) { $translations = $entity->getTranslationLanguages(FALSE); $original_translations = $entity->original->getTranslationLanguages(FALSE); $all_translations = array_keys($translations + $original_translations); // Notify modules of translation insertion/deletion. foreach ($all_translations as $langcode) { if (isset($translations[$langcode]) && !isset($original_translations[$langcode])) { $this->invokeHook('translation_insert', $entity->getTranslation($langcode)); } elseif (!isset($translations[$langcode]) && isset($original_translations[$langcode])) { $this->invokeHook('translation_delete', $entity->getTranslation($langcode)); } } } /** * Invokes a method on the Field objects within an entity. * * @param string $method * The method name. * @param \Drupal\Core\Entity\ContentEntityInterface $entity * The entity object. */ protected function invokeFieldMethod($method, ContentEntityInterface $entity) { foreach (array_keys($entity->getTranslationLanguages()) as $langcode) { $translation = $entity->getTranslation($langcode); foreach ($translation->getProperties(TRUE) as $field) { $field->$method(); } } } }