devel_wizard-2.x-dev/templates/spell/entity_type/content/list_builder.php.twig

templates/spell/entity_type/content/list_builder.php.twig
{%
  include '@devel_wizard/php/devel_wizard.php.file.header.php.twig'
  with {
    'namespace': content.namespace,
  }
%}

{%-
  include '@devel_wizard/php/devel_wizard.php.file.use_statements.php.twig'
  with {
    'useStatements': {
      'Drupal\\Core\\Entity\\EntityInterface': '',
      'Drupal\\Core\\Entity\\EntityListBuilder': '',
      'Drupal\\Core\\Entity\\EntityTypeInterface': '',
      'Drupal\\Core\\Entity\\EntityTypeManagerInterface': '',
      'Symfony\\Component\\DependencyInjection\\ContainerInterface': '',
    },
  }
%}

class ListBuilder extends EntityListBuilder {

  protected EntityTypeManagerInterface $entityTypeManager;

  /**
   * {@inheritdoc}
   *
   * @noinspection PhpMissingParentCallCommonInspection
   */
  public static function createInstance(
    ContainerInterface $container,
    EntityTypeInterface $entity_type,
  ): static {
    // @phpstan-ignore-next-line
    return new static(
      $entity_type,
      $container->get('entity_type.manager'),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function __construct(
    EntityTypeInterface $entity_type,
    EntityTypeManagerInterface $entityTypeManager,
  ) {
    $this->entityTypeManager = $entityTypeManager;

    /* @noinspection PhpUnhandledExceptionInspection */
    parent::__construct(
      $entity_type,
      $this->entityTypeManager->getStorage($entity_type->id()),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header = [
      'id' => $this->t('Id'),
    ];

    if ($this->entityType->getBundleEntityType()) {
      $header['bundle'] = $this->t('Type');
    }

    $header['label'] = $this->t('Label');

    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   *
   * @throws \Drupal\Core\Entity\EntityMalformedException
   */
  public function buildRow(EntityInterface $entity) {
    $bundleEntityTypeId = $entity
      ->getEntityType()
      ->getBundleEntityType();

    $bundle = NULL;
    if ($bundleEntityTypeId) {
      /* @noinspection PhpUnhandledExceptionInspection */
      $bundle = $this
        ->entityTypeManager
        ->getStorage($entity->getEntityType()->getBundleEntityType())
        ->load($entity->bundle());
    }

    $row = [
      'id' => [
        'data' => $entity->id(),
      ],
    ];

    if ($bundle) {
      $row['bundle'] = [
        'data' => ($bundle->access('edit') ? $bundle->toLink(NULL, 'edit-form') : $bundle->label()),
      ];
    }

    $row['label'] = [
      'data' => $entity->toLink(),
    ];

    return $row + parent::buildRow($entity);
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc