Commit 97a106e1 authored by Ilias Dimopoulos's avatar Ilias Dimopoulos Committed by Claudiu Cristea
Browse files

Issue #3270884 by idimopoulos, claudiu.cristea: Minor fixes to make the module more flexible

parent 26314b39
Loading
Loading
Loading
Loading
+56 −40
Original line number Diff line number Diff line
@@ -24,19 +24,28 @@ function meta_entity_entity_delete(EntityInterface $entity): void {
    return;
  }

  $entity_type_manager = \Drupal::entityTypeManager();
  foreach (\Drupal::getContainer()->getParameter('meta_entity.repositories') as $service_id => $meta_entity_type_id) {
    /** @var \Drupal\meta_entity\MetaEntityRepositoryInterface $repository */
  $repository = \Drupal::service('meta_entity.repository');
    $repository = \Drupal::service($service_id);
    if ($meta_entities = $repository->getMetaEntitiesForEntity($entity)) {
    \Drupal::entityTypeManager()->getStorage('meta_entity')->delete($meta_entities);
      $entity_type_manager->getStorage($meta_entity_type_id)->delete($meta_entities);
    }
  }
}

/**
 * Implements hook_ENTITY_TYPE_presave().
 * Implements hook_entity_presave().
 *
 * Sets a standard label if there's no current label.
 * Sets a standard label if there's no current label. Uses the generic
 * hook_entity_presave() instead of the more specific hook_ENTITY_TYPE_presave()
 * in order to allow other entities that implement the MetaEntityInterface have
 * the label automatically set.
 */
function meta_entity_meta_entity_presave(MetaEntityInterface $meta_entity): void {
function meta_entity_entity_presave(EntityInterface $meta_entity): void {
  if (!$meta_entity instanceof MetaEntityInterface) {
    return;
  }
  // If the meta entity has already a label we'll not overwrite it. Also, don't
  // compute a label if the target entity is not set.
  if (!empty($meta_entity->label()) || $meta_entity->get('target')->isEmpty()) {
@@ -63,14 +72,15 @@ function meta_entity_meta_entity_presave(MetaEntityInterface $meta_entity): void
 * field is configurable in the meta entity type level.
 */
function meta_entity_entity_bundle_field_info(EntityTypeInterface $entity_type, string $bundle, array $base_field_definitions): array {
  /** @var \Drupal\meta_entity\MetaEntityRepositoryInterface $repository */
  $repository = \Drupal::service('meta_entity.repository');
  /** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info */
  $bundle_info = \Drupal::service('entity_type.bundle.info');
  $entity_type_id = $entity_type->id();

  $fields = [];
  foreach ($repository->getReverseReferenceFieldNames($entity_type_id, $bundle) as $meta_entity_type_id => $field_name) {
  foreach (\Drupal::getContainer()->getParameter('meta_entity.repositories') as $service_id => $meta_entity_type_id) {
    /** @var \Drupal\meta_entity\MetaEntityRepositoryInterface $repository */
    $repository = \Drupal::service($service_id);
    foreach ($repository->getReverseReferenceFieldNames($entity_type_id, $bundle) as $meta_entity_bundle => $field_name) {
      /** @var \Drupal\Core\Field\BaseFieldDefinition $definition */
      $bundle_label = $bundle_info->getBundleInfo($entity_type_id)[$bundle]['label'];
      $fields[$field_name] = BaseFieldDefinition::create('entity_reference')
@@ -79,13 +89,13 @@ function meta_entity_entity_bundle_field_info(EntityTypeInterface $entity_type,
        ->setTargetBundle($bundle)
        ->setLabel(t('@label reference', ['@label' => $bundle_label]))
        ->setDescription(t('@label attached metadata.'))
      ->setSetting('target_type', 'meta_entity')
      ->setSetting('meta_entity_type_id', $meta_entity_type_id)
        ->setSetting('target_type', $meta_entity_type_id)
        ->setSetting('meta_entity_type_id', $meta_entity_bundle)
        ->setCardinality(1)
        ->setComputed(TRUE)
        ->setClass(MetaEntityReverseReferenceItemList::class);
    }

  }
  return $fields;
}

@@ -97,17 +107,22 @@ function meta_entity_entity_insert(EntityInterface $entity) {
    return;
  }

  $meta_entity_type_ids = \Drupal::service('meta_entity.repository')->getTypesWithAutoCreation($entity);
  if ($meta_entity_type_ids) {
    $storage = \Drupal::entityTypeManager()->getStorage('meta_entity');
    foreach ($meta_entity_type_ids as $meta_entity_type_id) {
  $entity_type_manager = \Drupal::entityTypeManager();
  foreach (\Drupal::getContainer()->getParameter('meta_entity.repositories') as $service_id => $meta_entity_type_id) {
    /** @var \Drupal\meta_entity\MetaEntityRepositoryInterface $repository */
    $repository = \Drupal::service($service_id);
    $meta_entity_bundles = $repository->getTypesWithAutoCreation($entity);
    if ($meta_entity_bundles) {
      $storage = $entity_type_manager->getStorage($meta_entity_type_id);
      foreach ($meta_entity_bundles as $meta_entity_bundle) {
        $meta_entity = $storage->create([
        'type' => $meta_entity_type_id,
          'type' => $meta_entity_bundle,
          'target' => $entity,
        ]);
        $meta_entity->save();
      \Drupal::logger('meta_entity')->debug("Auto-created %meta_entity_type_id meta-entity with ID @meta_entity_id for the %bundle %entity_type entity with ID @entity_id.", [
        '%meta_entity_type_id' => $meta_entity_type_id,
        \Drupal::logger('meta_entity')->debug("Auto-created %meta_entity_bundle @meta_entity_type_id with ID @meta_entity_id for the %bundle %entity_type entity with ID @entity_id.", [
          '%meta_entity_bundle' => $meta_entity_bundle,
          '@meta_entity_type_id' => str_replace('_', '-', $meta_entity_type_id),
          '@meta_entity_id' => $meta_entity->id(),
          '%bundle' => $entity->bundle(),
          '%entity_type' => $entity->getEntityTypeId(),
@@ -116,3 +131,4 @@ function meta_entity_entity_insert(EntityInterface $entity) {
      }
    }
  }
}
+1 −24
Original line number Diff line number Diff line
services:

  meta_entity.repository:
    class: Drupal\meta_entity\MetaEntityRepository
    arguments: ['@entity_type.manager', '@meta_entity.cache']

  meta_entity.cache:
    class: Drupal\Core\Cache\BackendChain
    calls:
      - [appendBackend, ['@meta_entity.memory_cache']]
      - [appendBackend, ['@meta_entity.persistent_cache']]
    tags:
      # This tag ensures that Drupal's cache_tags.invalidator service
      # invalidates also this cache data.
      - { name: cache.bin }
    # The service argument is deprecated in Drupal 8.8.0.
    # @see https://www.drupal.org/project/drupal/issues/3061117
    arguments: ['']

  meta_entity.persistent_cache:
    class: Drupal\Core\Cache\CacheBackendInterface
    tags:
      - { name: cache.bin }
    factory: cache_factory:get
    arguments: [meta_entity]

  meta_entity.memory_cache:
    class: Drupal\Core\Cache\MemoryCache\MemoryCache
      - { name: meta_entity.repository, meta_entity_type: meta_entity }
+22 −4
Original line number Diff line number Diff line
@@ -136,10 +136,28 @@ class MetaEntity extends ContentEntityBase implements MetaEntityInterface {
  /**
   * {@inheritdoc}
   */
  public static function loadOrCreate(string $meta_entity_type, ContentEntityInterface $target_entity): MetaEntityInterface {
    $storage = \Drupal::entityTypeManager()->getStorage('meta_entity');
  public static function loadOrCreate(string $meta_entity_bundle, ContentEntityInterface $target_entity): MetaEntityInterface {
    return self::loadOrCreateHelper('meta_entity', $meta_entity_bundle, $target_entity);
  }

  /**
   * Helper class to load or create the meta entity.
   *
   * @param string $meta_entity_type_id
   *   The meta entity type ID to be loaded or created.
   * @param string $meta_entity_bundle
   *   The meta entity type bundle to be loaded or created.
   * @param \Drupal\Core\Entity\ContentEntityInterface $target_entity
   *   The target content entity.
   *
   * @return \Drupal\meta_entity\Entity\MetaEntityInterface
   *   A meta entity.
   */
  protected static function loadOrCreateHelper(string $meta_entity_type_id, string $meta_entity_bundle, ContentEntityInterface $target_entity): MetaEntityInterface {
    $storage = \Drupal::entityTypeManager()->getStorage($meta_entity_type_id);
    $ids = $storage->getQuery()
      ->condition('type', $meta_entity_type)
      ->accessCheck(FALSE)
      ->condition('type', $meta_entity_bundle)
      ->condition('target.target_type', $target_entity->getEntityTypeId())
      ->condition('target.target_id', $target_entity->id())
      ->execute();
@@ -150,7 +168,7 @@ class MetaEntity extends ContentEntityBase implements MetaEntityInterface {
    }
    else {
      $meta_entity = $storage->create([
        'type' => $meta_entity_type,
        'type' => $meta_entity_bundle,
        'target' => $target_entity,
      ]);
    }
+3 −3
Original line number Diff line number Diff line
@@ -40,14 +40,14 @@ interface MetaEntityInterface extends ContentEntityInterface {
  /**
   * Loads or, if doesn't exists, creates a meta entity given a target entity.
   *
   * @param string $meta_entity_type
   *   The meta entity type to be loaded or created.
   * @param string $meta_entity_bundle
   *   The meta entity type bundle to be loaded or created.
   * @param \Drupal\Core\Entity\ContentEntityInterface $target_entity
   *   The target content entity.
   *
   * @return \Drupal\meta_entity\Entity\MetaEntityInterface
   *   A meta entity.
   */
  public static function loadOrCreate(string $meta_entity_type, ContentEntityInterface $target_entity): MetaEntityInterface;
  public static function loadOrCreate(string $meta_entity_bundle, ContentEntityInterface $target_entity): MetaEntityInterface;

}
+1 −0
Original line number Diff line number Diff line
@@ -239,6 +239,7 @@ class MetaEntityTypeForm extends BundleEntityFormBase {
            // Validate that this field name is unique within the same target
            // entity type and meta entity type.
            $ids = $storage->getQuery()
              ->accessCheck(FALSE)
              ->condition("mapping.{$entity_type_id}.*.field_name", $field_name)
              ->condition('id', $this->getEntity()->id(), '<>')
              ->execute();
Loading