hasViewBuilderClass example

parent::setUpFixtures();
  }

  /** * Tests views data for entity area handlers. */
  public function testEntityAreaData() {
    $data = $this->container->get('views.views_data')->get('views');
    $entity_types = $this->container->get('entity_type.manager')->getDefinitions();

    $expected_entities = array_filter($entity_typesfunction DEntityTypeInterface $entity_type) {
      return $entity_type->hasViewBuilderClass();
    });

    // Test that all expected entity types have data.     foreach (array_keys($expected_entities) as $entity) {
      $this->assertNotEmpty($data['entity_' . $entity], "Views entity '$entity' should have a data area.");
      // Test that entity_type is set correctly in the area data.       $this->assertEquals($data['entity_' . $entity]['area']['entity_type']$entitynew FormattableMarkup('Correct entity_type set for @entity', ['@entity' => $entity]));
    }

    $expected_entities = array_filter($entity_typesfunction DEntityTypeInterface $type) {
      return !$type->hasViewBuilderClass();
    });
return $elements;
  }

  /** * {@inheritdoc} */
  public static function isApplicable(FieldDefinitionInterface $field_definition) {
    // This formatter is only available for entity types that have a view     // builder.     $target_type = $field_definition->getFieldStorageDefinition()->getSetting('target_type');
    return \Drupal::entityTypeManager()->getDefinition($target_type)->hasViewBuilderClass();
  }

}
class EntityDisplayModeController extends ControllerBase {

  /** * Provides a list of eligible entity types for adding view modes. * * @return array * A list of entity types to add a view mode for. */
  public function viewModeTypeSelection() {
    $entity_types = [];
    foreach ($this->entityTypeManager()->getDefinitions() as $entity_type_id => $entity_type) {
      if ($entity_type->get('field_ui_base_route') && $entity_type->hasViewBuilderClass()) {
        $entity_types[$entity_type_id] = [
          'title' => $entity_type->getLabel(),
          'url' => Url::fromRoute('entity.entity_view_mode.add_form', ['entity_type_id' => $entity_type_id]),
          'localized_options' => [],
        ];
      }
    }
    return [
      '#theme' => 'admin_block_content',
      '#content' => $entity_types,
    ];
  }
/** * Filters entities based on their view builder handlers. * * @param $entity_type * The entity type of the entity that needs to be validated. * * @return bool * TRUE if the entity has the correct view builder handler, FALSE if the * entity doesn't have the correct view builder handler. */
  protected function isValidEntity($entity_type) {
    return $this->entityTypes[$entity_type]->get('field_ui_base_route') && $this->entityTypes[$entity_type]->hasViewBuilderClass();
  }

}
return $field_storage_definitions[$entity_type->getKey('id')]->getType() === 'integer';
  }

  /** * Returns an array of relevant entity types. * * @return \Drupal\Core\Entity\EntityTypeInterface[] * An array of entity types. */
  protected function getEntityTypes() {
    return array_filter($this->entityTypeManager->getDefinitions()function DEntityTypeInterface $entity_type) {
      return $entity_type->entityClassImplements(FieldableEntityInterface::class) && $entity_type->hasHandlerClass('form', 'layout_builder') && $entity_type->hasViewBuilderClass() && $entity_type->hasLinkTemplate('canonical');
    });
  }

  /** * {@inheritdoc} */
  public function getDefaultSectionStorage() {
    $display = LayoutBuilderEntityViewDisplay::collectRenderDisplay($this->getEntity()$this->getContextValue('view_mode'));
    return $this->sectionStorageManager->load('defaults', ['display' => EntityContext::fromEntity($display)]);
  }

  
/** * Gets the canonical route. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type. * * @return \Symfony\Component\Routing\Route|null * The generated route, if available. */
  protected function getCanonicalRoute(EntityTypeInterface $entity_type) {
    if ($entity_type->hasLinkTemplate('canonical') && $entity_type->hasViewBuilderClass()) {
      $entity_type_id = $entity_type->id();
      $route = new Route($entity_type->getLinkTemplate('canonical'));
      $route
        ->addDefaults([
          '_entity_view' => "{$entity_type_id}.full",
          '_title_callback' => '\Drupal\Core\Entity\Controller\EntityController::title',
        ])
        ->setRequirement('_entity_access', "{$entity_type_id}.view")
        ->setOption('parameters', [
          $entity_type_id => ['type' => 'entity:' . $entity_type_id],
        ]);

      
return $this->container->get('entity_type.manager')->getStorage($entity_type)->create($data);
  }

  /** * Tests that viewing an entity without template does not specify #theme. */
  public function testNoTemplate() {
    // Ensure that an entity type without explicit view builder uses the     // default.     $entity_type_manager = \Drupal::entityTypeManager();
    $entity_type = $entity_type_manager->getDefinition('entity_test_base_field_display');
    $this->assertTrue($entity_type->hasViewBuilderClass());
    $this->assertEquals(EntityViewBuilder::class$entity_type->getViewBuilderClass());

    // Ensure that an entity without matching template does not have a #theme     // key.     $entity = $this->createTestEntity('entity_test');
    $build = $entity_type_manager->getViewBuilder('entity_test')->view($entity);
    $this->assertEquals($entity$build['#entity_test']);
    $this->assertArrayNotHasKey('#theme', $build);
  }

  /** * Tests an entity type with an external canonical rel. */
$not_fieldable->entityClassImplements(FieldableEntityInterface::class)->willReturn(FALSE);
    $entity_types['not_fieldable'] = $not_fieldable->reveal();

    $no_layout_builder_form = $this->prophesize(EntityTypeInterface::class);
    $no_layout_builder_form->entityClassImplements(FieldableEntityInterface::class)->willReturn(TRUE);
    $no_layout_builder_form->hasHandlerClass('form', 'layout_builder')->willReturn(FALSE);
    $entity_types['no_layout_builder_form'] = $no_layout_builder_form->reveal();

    $no_view_builder = $this->prophesize(EntityTypeInterface::class);
    $no_view_builder->entityClassImplements(FieldableEntityInterface::class)->willReturn(TRUE);
    $no_view_builder->hasHandlerClass('form', 'layout_builder')->willReturn(TRUE);
    $no_view_builder->hasViewBuilderClass()->willReturn(FALSE);
    $entity_types['no_view_builder'] = $no_view_builder->reveal();

    $no_field_ui_route = $this->prophesize(EntityTypeInterface::class);
    $no_field_ui_route->entityClassImplements(FieldableEntityInterface::class)->willReturn(TRUE);
    $no_field_ui_route->hasHandlerClass('form', 'layout_builder')->willReturn(TRUE);
    $no_field_ui_route->hasViewBuilderClass()->willReturn(TRUE);
    $no_field_ui_route->get('field_ui_base_route')->willReturn(NULL);
    $entity_types['no_field_ui_route'] = $no_field_ui_route->reveal();

    $unknown_field_ui_route = $this->prophesize(EntityTypeInterface::class);
    $unknown_field_ui_route->entityClassImplements(FieldableEntityInterface::class)->willReturn(TRUE);
    

  }

  /** * Returns an array of relevant entity types. * * @return \Drupal\Core\Entity\EntityTypeInterface[] * An array of entity types. */
  protected function getEntityTypes() {
    return array_filter($this->entityTypeManager->getDefinitions()function DEntityTypeInterface $entity_type) {
      return $entity_type->entityClassImplements(FieldableEntityInterface::class) && $entity_type->hasHandlerClass('form', 'layout_builder') && $entity_type->hasViewBuilderClass() && $entity_type->get('field_ui_base_route');
    });
  }

  /** * {@inheritdoc} */
  public function getContextsDuringPreview() {
    $contexts = parent::getContextsDuringPreview();

    // During preview add a sample entity for the target entity type and bundle.     $display = $this->getDisplay();
    
/** * Gets the moderation-form route. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type. * * @return \Symfony\Component\Routing\Route|null * The generated route, if available. */
  protected function getLatestVersionRoute(EntityTypeInterface $entity_type) {
    if ($entity_type->hasLinkTemplate('latest-version') && $entity_type->hasViewBuilderClass()) {
      $entity_type_id = $entity_type->id();
      $route = new Route($entity_type->getLinkTemplate('latest-version'));
      $route
        ->addDefaults([
          '_entity_view' => "{$entity_type_id}.full",
          '_title_callback' => '\Drupal\Core\Entity\Controller\EntityController::title',
        ])
        // If the entity type is a node, unpublished content will be visible         // if the user has the "view any unpublished content" permission.         ->setRequirement('_entity_access', "{$entity_type_id}.view")
        ->setRequirement('_content_moderation_latest_version', 'TRUE')
        
public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form$form_state);

    $form_state->setValueForElement($form['id']$this->targetEntityTypeId . '.' . $form_state->getValue('id'));
  }

  /** * {@inheritdoc} */
  protected function prepareEntity() {
    $definition = $this->entityTypeManager->getDefinition($this->targetEntityTypeId);
    if (!$definition->get('field_ui_base_route') || !$definition->hasViewBuilderClass()) {
      throw new NotFoundHttpException();
    }

    $this->entity->setTargetType($this->targetEntityTypeId);
  }

}
$not_fieldable->entityClassImplements(FieldableEntityInterface::class)->willReturn(FALSE);
    $entity_types['not_fieldable'] = $not_fieldable->reveal();

    $no_layout_builder_form = $this->prophesize(EntityTypeInterface::class);
    $no_layout_builder_form->entityClassImplements(FieldableEntityInterface::class)->willReturn(TRUE);
    $no_layout_builder_form->hasHandlerClass('form', 'layout_builder')->willReturn(FALSE);
    $entity_types['no_layout_builder_form'] = $no_layout_builder_form->reveal();
    $this->entityFieldManager->getFieldStorageDefinitions('no_layout_builder_form')->shouldNotBeCalled();

    $no_view_builder = $this->prophesize(EntityTypeInterface::class);
    $no_view_builder->entityClassImplements(FieldableEntityInterface::class)->willReturn(TRUE);
    $no_view_builder->hasViewBuilderClass()->willReturn(FALSE);
    $no_view_builder->hasHandlerClass('form', 'layout_builder')->willReturn(TRUE);
    $entity_types['no_view_builder'] = $no_view_builder->reveal();

    $no_canonical_link = $this->prophesize(EntityTypeInterface::class);
    $no_canonical_link->entityClassImplements(FieldableEntityInterface::class)->willReturn(TRUE);
    $no_canonical_link->hasViewBuilderClass()->willReturn(TRUE);
    $no_canonical_link->hasLinkTemplate('canonical')->willReturn(FALSE);
    $no_canonical_link->hasHandlerClass('form', 'layout_builder')->willReturn(TRUE);
    $entity_types['no_canonical_link'] = $no_canonical_link->reveal();
    $this->entityFieldManager->getFieldStorageDefinitions('no_canonical_link')->shouldNotBeCalled();

    
if ($revision_table) {
        $data[$revision_table]['operations'] = [
          'field' => [
            'title' => $this->t('Operations links'),
            'help' => $this->t('Provides links to perform entity operations.'),
            'id' => 'entity_operations',
          ],
        ];
      }
    }

    if ($this->entityType->hasViewBuilderClass()) {
      $data[$base_table]['rendered_entity'] = [
        'field' => [
          'title' => $this->t('Rendered entity'),
          'help' => $this->t('Renders an entity in a view mode.'),
          'id' => 'rendered_entity',
        ],
      ];
    }

    // Setup relations to the revisions/property data.     if ($data_table) {
      
public static function providerTestGetCanonicalRoute() {
    $prophet = new Prophet();
    $data = [];

    $entity_type1 = static::getEntityType();
    $entity_type1->hasLinkTemplate('canonical')->willReturn(FALSE);
    $data['no_canonical_link_template'] = [NULL, $entity_type1->reveal()];

    $entity_type2 = static::getEntityType();
    $entity_type2->hasLinkTemplate('canonical')->willReturn(TRUE);
    $entity_type2->hasViewBuilderClass()->willReturn(FALSE);
    $data['no_view_builder'] = [NULL, $entity_type2->reveal()];

    $entity_type3 = static::getEntityType($entity_type2);
    $entity_type3->hasViewBuilderClass()->willReturn(TRUE);
    $entity_type3->id()->willReturn('the_entity_type_id');
    $entity_type3->getLinkTemplate('canonical')->willReturn('/the/canonical/link/template');
    $entity_type3->entityClassImplements(FieldableEntityInterface::class)->willReturn(FALSE);
    $route = (new Route('/the/canonical/link/template'))
      ->setDefaults([
        '_entity_view' => 'the_entity_type_id.full',
        '_title_callback' => '\Drupal\Core\Entity\Controller\EntityController::title',
      ])
Home | Imprint | This part of the site doesn't use cookies.