getBundleOf example

$prophecy = $this->prophesize(EntityTypeManagerInterface::class);
    $prophecy->getDefinition('entity_type')
      ->willReturn($entity_type);
    $entity_type_manager = $prophecy->reveal();

    $bundle_form = new EntityPermissionsForm($permission_handler$role_storage$module_handler$config_manager$entity_type_manager);

    // Mock the method parameters.     $route = new Route('some.path');
    $route_match = $this->prophesize(RouteMatchInterface::class)->reveal();
    $prophecy = $this->prophesize(EntityTypeInterface::class);
    $prophecy->getBundleOf()
      ->willReturn('entity_type');
    $bundle_type = $prophecy->reveal();
    $prophecy = $this->prophesize(EntityInterface::class);
    $prophecy->getEntityType()
      ->willReturn($bundle_type);
    $prophecy->getConfigDependencyName()
      ->willReturn('node.type.article');
    $bundle = $prophecy->reveal();

    $access_actual = $bundle_form->access($route$route_match$bundle);
    $this->assertEquals($found ? AccessResult::allowed() : AccessResult::neutral()$access_actual);
  }

  public function getEntityType() {
    return $this->entityTypeManager()->getDefinition($this->getEntityTypeId());
  }

  /** * {@inheritdoc} */
  public function preSave(EntityStorageInterface $storage) {
    // Check if this is an entity bundle.     if ($this->getEntityType()->getBundleOf()) {
      // Throw an exception if the bundle ID is longer than 32 characters.       if (mb_strlen($this->id()) > EntityTypeInterface::BUNDLE_MAX_LENGTH) {
        throw new ConfigEntityIdLengthException("Attempt to create a bundle with an ID longer than " . EntityTypeInterface::BUNDLE_MAX_LENGTH . " characters: $this->id().");
      }
    }
  }

  /** * {@inheritdoc} */
  public function postSave(EntityStorageInterface $storage$update = TRUE) {
    

  public function formAlter(array &$form, FormStateInterface $form_state$form_id) {
    $form_object = $form_state->getFormObject();
    if ($form_object instanceof BundleEntityFormBase) {
      $config_entity = $form_object->getEntity();
      $bundle_of = $config_entity->getEntityType()->getBundleOf();
      if ($bundle_of
          && ($bundle_of_entity_type = $this->entityTypeManager->getDefinition($bundle_of))
          && $this->moderationInfo->shouldModerateEntitiesOfBundle($bundle_of_entity_type$config_entity->id())) {
        $this->entityTypeManager->getHandler($bundle_of, 'moderation')->enforceRevisionsBundleFormAlter($form$form_state$form_id);
      }
    }
    elseif ($this->isModeratedEntityEditForm($form_object)) {
      /** @var \Drupal\Core\Entity\ContentEntityFormInterface $form_object */
      /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
      $entity = $form_object->getEntity();

      
$storage->delete($displays);
    }
  }

  /** * {@inheritdoc} */
  public function postSave(EntityStorageInterface $storage$update = TRUE) {
    parent::postSave($storage$update);

    $entity_type_manager = $this->entityTypeManager();
    $bundle_of = $this->getEntityType()->getBundleOf();
    if (!$update) {
      \Drupal::service('entity_bundle.listener')->onBundleCreate($this->id()$bundle_of);
    }
    else {
      // Invalidate the render cache of entities for which this entity       // is a bundle.       if ($entity_type_manager->hasHandler($bundle_of, 'view_builder')) {
        $entity_type_manager->getViewBuilder($bundle_of)->resetCache();
      }
      // Entity bundle field definitions may depend on bundle settings.       \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
      

  protected function getEntityPermissionsRoute(EntityTypeInterface $entity_type): ?Route {
    if (!$entity_type->hasLinkTemplate('entity-permissions-form')) {
      return NULL;
    }

    if (!$bundle_of_id = $entity_type->getBundleOf()) {
      return NULL;
    }

    $entity_type_id = $entity_type->id();
    $route = new Route(
      $entity_type->getLinkTemplate('entity-permissions-form'),
      [
        '_title' => 'Manage permissions',
        '_form' => 'Drupal\user\Form\EntityPermissionsForm',
        'entity_type_id' => $bundle_of_id,
        'bundle_entity_type' => $entity_type_id,
      ],
'unsigned' => TRUE,
        ],
      ];
    }
    else {
      $columns = [
        'target_id' => [
          'description' => 'The ID of the target entity.',
          'type' => 'varchar_ascii',
          // If the target entities act as bundles for another entity type,           // their IDs should not exceed the maximum length for bundles.           'length' => $target_type_info->getBundleOf() ? EntityTypeInterface::BUNDLE_MAX_LENGTH : 255,
        ],
      ];
    }

    $schema = [
      'columns' => $columns,
      'indexes' => [
        'target_id' => ['target_id'],
      ],
    ];

    

  protected function setUp(): void {
    parent::setUp();

    $prophecy = $this->prophesize(EntityTypeInterface::class);
    $prophecy->hasLinkTemplate('entity-permissions-form')->willReturn(FALSE);
    $entity_no_link_template = $prophecy->reveal();

    $prophecy = $this->prophesize(EntityTypeInterface::class);
    $prophecy->hasLinkTemplate('entity-permissions-form')->willReturn(TRUE);
    $prophecy->getBundleOf()->willReturn(NULL);
    $entity_no_bundle_of = $prophecy->reveal();

    $prophecy = $this->prophesize(EntityTypeInterface::class);
    $prophecy->hasLinkTemplate('entity-permissions-form')->willReturn(TRUE);
    $prophecy->getBundleOf()->willReturn('content_entity_type_id');
    $entity_bundle_of = $prophecy->reveal();

    $prophecy = $this->prophesize(EntityTypeInterface::class);
    $prophecy->hasLinkTemplate('entity-permissions-form')->willReturn(FALSE);
    $prophecy->get('field_ui_base_route')->willReturn('field_ui.base_route');
    $content_entity_type = $prophecy->reveal();

    


  /** * {@inheritdoc} */
  public function onDependencyRemoval(array $dependencies) {
    $changed = parent::onDependencyRemoval($dependencies);

    // When bundle config entities are removed, ensure they are cleaned up from     // the workflow.     foreach ($dependencies['config'] as $removed_config) {
      if ($entity_type_id = $removed_config->getEntityType()->getBundleOf()) {
        $bundle_id = $removed_config->id();
        $this->removeEntityTypeAndBundle($entity_type_id$bundle_id);
        $changed = TRUE;
      }
    }

    // When modules that provide entity types are removed, ensure they are also     // removed from the workflow.     if (!empty($dependencies['module'])) {
      // Gather all entity definitions provided by the dependent modules which       // are being removed.
$this->entityTypeManager->getDefinitions()->willReturn($definitions);
  }

  /** * Tests the getEntityTypeLabels() method. * * @covers ::getEntityTypeLabels */
  public function testGetEntityTypeLabels() {
    $apple = $this->prophesize(EntityTypeInterface::class);
    $apple->getLabel()->willReturn('Apple');
    $apple->getBundleOf()->willReturn(NULL);

    $banana = $this->prophesize(EntityTypeInterface::class);
    $banana->getLabel()->willReturn('Banana');
    $banana->getBundleOf()->willReturn(NULL);

    $this->setUpEntityTypeDefinitions([
      'apple' => $apple,
      'banana' => $banana,
    ]);

    $expected = [
      

  public function getDerivativeDefinitions($base_plugin_definition) {
    $this->derivatives = [];

    $entity_definitions = $this->entityTypeManager->getDefinitions();
    foreach ($entity_definitions as $bundle_type_id => $bundle_entity_type) {
      if (!$bundle_entity_type->hasLinkTemplate('entity-permissions-form')) {
        continue;
      }

      if (!$entity_type_id = $bundle_entity_type->getBundleOf()) {
        continue;
      }

      $entity_type = $entity_definitions[$entity_type_id];
      if (!$base_route = $entity_type->get('field_ui_base_route')) {
        continue;
      }

      $this->derivatives["permissions_$bundle_type_id"] = [
        'route_name' => "entity.$bundle_type_id.entity_permissions_form",
        'weight' => 10,
        

  public function onConfigImporterValidate(ConfigImporterEvent $event) {
    foreach ($event->getChangelist('delete') as $config_name) {
      // Get the config entity type ID. This also ensure we are dealing with a       // configuration entity.       if ($entity_type_id = $this->configManager->getEntityTypeIdByName($config_name)) {
        $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
        // Does this entity type define a bundle of another entity type.         if ($bundle_of = $entity_type->getBundleOf()) {
          // Work out if there are entities with this bundle.           $bundle_of_entity_type = $this->entityTypeManager->getDefinition($bundle_of);
          $bundle_id = ConfigEntityStorage::getIDFromConfigName($config_name$entity_type->getConfigPrefix());
          $entity_query = $this->entityTypeManager->getStorage($bundle_of)->getQuery();
          $entity_ids = $entity_query->condition($bundle_of_entity_type->getKey('bundle')$bundle_id)
            ->accessCheck(FALSE)
            ->range(0, 1)
            ->execute();
          if (!empty($entity_ids)) {
            $entity = $this->entityTypeManager->getStorage($entity_type_id)->load($bundle_id);
            $event->getConfigImporter()->logError($this->t('Entities exist of type %entity_type and %bundle_label %bundle. These entities need to be deleted before importing.', ['%entity_type' => $bundle_of_entity_type->getLabel(), '%bundle_label' => $bundle_of_entity_type->getBundleLabel(), '%bundle' => $entity->label()]));
          }
Home | Imprint | This part of the site doesn't use cookies.