Verified Commit c1781d3b authored by Lee Rowlands's avatar Lee Rowlands Committed by Lee Rowlands
Browse files

Issue #3212677 by larowlan: Replace the default list builder with a view

parent 603c21cb
Loading
Loading
Loading
Loading
+688 −134

File changed.

Preview size limit exceeded, changes collapsed.

+1 −1
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface;
 *   handlers = {
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" = \Drupal\filter_format_audit\AnalysisResultListBuilder::class,
 *     "views_data" = "Drupal\views\EntityViewsData",
 *     "views_data" = \Drupal\filter_format_audit\EntityHandlers\AnalysisResultViewsData::class,
 *     "route_provider" = {
 *       "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
 *     }
+25 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\filter_format_audit\EntityHandlers;

use Drupal\views\EntityViewsData;

/**
 * Defines a class for providing views data for analysis result entities.
 */
final class AnalysisResultViewsData extends EntityViewsData {

  /**
   * {@inheritdoc}
   */
  public function getViewsData(): array {
    $data = parent::getViewsData();
    $base_table = $this->entityType->getBaseTable() ?: $this->entityType->id();
    $data[$base_table]['field_name']['field']['id'] = 'filter_format_audit_field';
    $data[$base_table]['content__target_id']['field']['id'] = 'filter_format_audit_content';
    return $data;
  }

}
+57 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\filter_format_audit\Plugin\views\field;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\filter_format_audit\EntityHandlers\FilterFormatAuditHandlerInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Defines a views field plugin for the content entity reference.
 *
 * @ViewsField("filter_format_audit_content")
 */
final class ContentEntityReference extends FieldPluginBase {

  /**
   * Entity type manager.
   */
  protected EntityTypeManagerInterface $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
    $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    $instance->entityTypeManager = $container->get('entity_type.manager');
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
    $entity = $this->getEntity($values);
    $content_entity = $entity->get('content')->entity;
    if (!$content_entity) {
      return $this->t('Deleted');
    }
    $handler = $this->entityTypeManager->getHandler($content_entity->getEntityTypeId(), FilterFormatAuditHandlerInterface::HANDLER_TYPE);
    assert($handler instanceof FilterFormatAuditHandlerInterface);
    $url = $handler->getUrl($content_entity);
    $label = $handler->getLabel($content_entity);
    if ($url) {
      return [
        '#type' => 'link',
        '#url' => $url,
        '#title' => $label,
      ];
    }
    return $label;
  }

}
+34 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\filter_format_audit\Plugin\views\field;

use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;

/**
 * Defines a views field plugin for the field name in an analysis result.
 *
 * @ViewsField("filter_format_audit_field")
 */
final class FieldName extends FieldPluginBase {

  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
    $entity = $this->getEntity($values);
    $content_entity = $entity->get('content')->entity;
    $field_name = $entity->field_name->value;
    if (!$content_entity) {
      return $field_name;
    }
    $field_definition = $content_entity->getFieldDefinition($field_name);
    if ($field_definition) {
      return $field_definition->getLabel();
    }
    return $field_name;
  }

}