ConfigEntityBundleBase.php 3.44 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains Drupal\Core\Config\Entity\ConfigEntityBundleBase.
 */

namespace Drupal\Core\Config\Entity;

10 11
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
12 13 14 15 16 17 18 19 20 21
use Drupal\Core\Entity\EntityStorageInterface;

/**
 * A base class for config entity types that act as bundles.
 *
 * Entity types that want to use this base class must use bundle_of in their
 * annotation to specify for which entity type they are providing bundles for.
 */
abstract class ConfigEntityBundleBase extends ConfigEntityBase {

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  /**
   * Renames displays when a bundle is renamed.
   */
  protected function renameDisplays() {
    // Rename entity displays.
    if ($this->getOriginalId() !== $this->id()) {
      foreach ($this->loadDisplays('entity_view_display') as $display) {
        $new_id = $this->getEntityType()->getBundleOf() . '.' . $this->id() . '.' . $display->mode;
        $display->set('id', $new_id);
        $display->bundle = $this->id();
        $display->save();
      }
    }

    // Rename entity form displays.
    if ($this->getOriginalId() !== $this->id()) {
      foreach ($this->loadDisplays('entity_form_display') as $form_display) {
        $new_id = $this->getEntityType()->getBundleOf() . '.' . $this->id() . '.' . $form_display->mode;
        $form_display->set('id', $new_id);
        $form_display->bundle = $this->id();
        $form_display->save();
      }
    }
  }

  /**
   * Deletes display if a bundle is deleted.
   */
  protected function deleteDisplays() {
    // Remove entity displays of the deleted bundle.
    if ($displays = $this->loadDisplays('entity_view_display')) {
      $storage = $this->entityManager()->getStorage('entity_view_display');
      $storage->delete($displays);
    }

    // Remove entity form displays of the deleted bundle.
    if ($displays = $this->loadDisplays('entity_form_display')) {
      $storage = $this->entityManager()->getStorage('entity_form_display');
      $storage->delete($displays);
    }
  }

64 65 66 67 68 69 70 71 72 73
  /**
   * {@inheritdoc}
   */
  public function postSave(EntityStorageInterface $storage, $update = TRUE) {
    parent::postSave($storage, $update);

    if (!$update) {
      entity_invoke_bundle_hook('create', $this->getEntityType()->getBundleOf(), $this->id());
    }
    elseif ($this->getOriginalId() != $this->id()) {
74
      $this->renameDisplays();
75 76 77 78 79 80 81 82 83 84 85
      entity_invoke_bundle_hook('rename', $this->getEntityType()->getBundleOf(), $this->getOriginalId(), $this->id());
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function postDelete(EntityStorageInterface $storage, array $entities) {
    parent::postDelete($storage, $entities);

    foreach ($entities as $entity) {
86
      $entity->deleteDisplays();
87 88 89 90
      entity_invoke_bundle_hook('delete', $entity->getEntityType()->getBundleOf(), $entity->id());
    }
  }

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  /**
   * Returns view or form displays for this bundle.
   *
   * @param string $entity_type_id
   *   The entity type ID of the display type to load.
   *
   * @return \Drupal\Core\Entity\Display\EntityDisplayInterface[]
   *   A list of matching displays.
   */
  protected function loadDisplays($entity_type_id) {
    $ids = \Drupal::entityQuery($entity_type_id)
      ->condition('id', $this->getEntityType()->getBundleOf() . '.' . $this->getOriginalId() . '.', 'STARTS_WITH')
      ->execute();
    if ($ids) {
      $storage = $this->entityManager()->getStorage($entity_type_id);
      return $storage->loadMultiple($ids);
    }
    return array();
  }

111
}