Commit 5a3f56f6 authored by Tom Keitel's avatar Tom Keitel
Browse files

Issue #3253672 by hctom: Improve code of formatter plugin classes

parent 32ef4a82
Loading
Loading
Loading
Loading
+3 −7
Changes for src/Plugin/Field/FieldFormatter/ViewModeSwitchDefaultFormatter.php: 3 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -20,21 +20,17 @@ class ViewModeSwitchDefaultFormatter extends ViewModeSwitchFormatterBase {

  /**
   * {@inheritdoc}
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   */
  public function viewElements(FieldItemListInterface $items, $langcode): array {
    $elements = [];
    $entity = $items->getEntity();

    /** @var \Drupal\view_mode_switch\Plugin\Field\FieldType\ViewModeSwitchItemInterface $item */
    foreach ($items as $delta => $item) {
      // Load view mode.
      $view_mode = $this->entityTypeManager->getStorage('entity_view_mode')
        ->load($entity->getEntityTypeId() . '.' . $item->value);
      $value = $item->value ?? '';

      // View mode found -> display its human-readable label.
      if ($view_mode) {
      if ($value === $item->getViewMode() && ($view_mode = $this->loadViewMode($entity, $value))) {
        $elements[$delta] = [
          '#markup' => $view_mode->label(),
          '#allowed_tags' => FieldFilteredMarkup::allowedTags(),
+36 −10
Changes for src/Plugin/Field/FieldFormatter/ViewModeSwitchFormatterBase.php: 36 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -2,7 +2,9 @@

namespace Drupal\view_mode_switch\Plugin\Field\FieldFormatter;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
use Drupal\Core\Entity\EntityViewModeInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FormatterInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
@@ -14,11 +16,11 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class ViewModeSwitchFormatterBase extends FormatterBase implements ContainerFactoryPluginInterface {

  /**
   * The entity type manager.
   * The entity view mode storage.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
   */
  protected $entityTypeManager;
  protected $entityViewModeStorage;

  /**
   * {@inheritdoc}
@@ -27,22 +29,46 @@ abstract class ViewModeSwitchFormatterBase extends FormatterBase implements Cont
    $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);

    // Inject entity type manager.
    $instance->setEntityTypeManager($container->get('entity_type.manager'));
    /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
    $entity_type_manager = $container->get('entity_type.manager');

    /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $entity_view_mode_storage */
    $entity_view_mode_storage = $entity_type_manager->getStorage('entity_view_mode');

    $instance->setEntityViewModeStorage($entity_view_mode_storage);

    return $instance;
  }

  /**
   * Sets the entity type manager.
   * Loads the view mode configuration for the given entity type and mode.
   *
   * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
   *   A fieldable entity.
   * @param string $name
   *   The name of the entity view mode to load.
   *
   * @return \Drupal\Core\Entity\EntityViewModeInterface|null
   *   The entity view mode on success, otherwise NULL.
   */
  protected function loadViewMode(FieldableEntityInterface $entity, string $name): ?EntityViewModeInterface {
    $entity_view_display = $this->entityViewModeStorage
      ->load($entity->getEntityTypeId() . '.' . $name);

    return $entity_view_display instanceof EntityViewModeInterface ? $entity_view_display : NULL;
  }

  /**
   * Sets the entity view mode storage.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage
   *   The entity view mode storage.
   *
   * @return static
   *   The object itself for chaining.
   */
  public function setEntityTypeManager(EntityTypeManagerInterface $entity_type_manager): FormatterInterface {
    $this->entityTypeManager = $entity_type_manager;
  public function setEntityViewModeStorage(ConfigEntityStorageInterface $storage): FormatterInterface {
    $this->entityViewModeStorage = $storage;

    return $this;
  }
+2 −9
Changes for src/Plugin/Field/FieldFormatter/ViewModeSwitchMachineNameFormatter.php: 2 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -20,23 +20,16 @@ class ViewModeSwitchMachineNameFormatter extends ViewModeSwitchFormatterBase {

  /**
   * {@inheritdoc}
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   */
  public function viewElements(FieldItemListInterface $items, $langcode): array {
    $elements = [];
    $entity = $items->getEntity();

    /** @var \Drupal\view_mode_switch\Plugin\Field\FieldType\ViewModeSwitchItemInterface $item */
    foreach ($items as $delta => $item) {
      $value = $item->value;

      /** @var \Drupal\Core\Entity\Entity\EntityViewMode|null $view_mode */
      $view_mode = $this->entityTypeManager->getStorage('entity_view_mode')
        ->load($entity->getEntityTypeId() . '.' . $value);

      // View mode found -> display its machine name.
      if ($view_mode) {
      if ($value === $item->getViewMode()) {
        $elements[$delta] = [
          '#markup' => $value,
          '#allowed_tags' => FieldFilteredMarkup::allowedTags(),