reviewer-1.2.x-dev/modules/reviewer_test_kit/src/Plugin/reviewer/Task/Entity/Display/DisplayTaskBase.php

modules/reviewer_test_kit/src/Plugin/reviewer/Task/Entity/Display/DisplayTaskBase.php
<?php

declare(strict_types=1);

namespace Drupal\reviewer_test_kit\Plugin\reviewer\Task\Entity\Display;

use Drupal\Core\Entity\Display\EntityDisplayInterface;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\reviewer_test_kit\Plugin\reviewer\Task\Entity\EntityTypeTaskBase;

/**
 * Base task for checking entity display configuration values.
 *
 * While it may be occasionally useful to extend this class, there is likely a
 * different reviewer test kit task that could be used to implement checks with
 * much less effort instead.
 *
 * Tasks extending this class must define the type of display being checked in
 * the $displayType property. If the $displayType is not set, a \LogicException
 * will be thrown.
 *
 * Here is an example of a fixable task which checks that the teaser view mode
 * is disabled:
 *
 * @code
 * #[Task('teaser_disabled')]
 * final class TeaserDisabled extends DisplayTaskBase implements FixableInterface {
 *
 *   protected string $displayType = 'view';
 *
 *   public function check(): ResultInterface {
 *     return $this->createCheckResult(
 *       !\in_array('teaser', $this->enabledViewDisplayNames(), TRUE),
 *       "Teaser view mode is not enabled for {$this->entityTypeFullId()}.",
 *       "Teaser view mode is enabled for {$this->entityTypeFullId()}.",
 *     );
 *   }
 *
 *   public function fix(): ResultInterface {
 *     $this->viewDisplay('teaser')->disable()->save();
 *
 *     return $this->createFixResult(
 *       $this->check()->getStatus(),
 *       "Disabled the teaser view mode for {$this->entityTypeFullId()}.",
 *       "Unable to disable the teaser view mode for {$this->entityTypeFullId()}.",
 *     );
 *   }
 *
 * }
 * @endcode
 */
abstract class DisplayTaskBase extends EntityTypeTaskBase {

  /**
   * The display type the task interacts with.
   *
   * \LogicException will be thrown if this property is empty.
   *
   * @var 'form'|'view'|''
   */
  protected string $displayType = '';

  /**
   * Callback to get fields for check and fix methods.
   *
   * Allows for children to override the get fields behavior, which is useful
   * when the task must get other properties, like third party settings.
   *
   * @return array<string, mixed[]>
   */
  protected function fieldsCallback(EntityDisplayInterface $display): array {
    return $display->getComponents();
  }

  /**
   * Save callback for the fix method.
   *
   * Allows for children to override the save behavior, which is useful when the
   * task must alter other properties, like third party settings.
   *
   * @param \Drupal\Core\Entity\Display\EntityDisplayInterface $display
   * @param string $field
   * @param array<string, array|scalar> $settings
   */
  protected function fixSaveCallback(
    EntityDisplayInterface $display,
    string $field,
    array $settings,
  ): void {
    $display->setComponent($field, $settings);
  }

  /**
   * Check that all properties on the task have valid values.
   *
   * @throws \LogicException
   *   Thrown when properties do not have the correct values.
   */
  protected function checkValidProperties(): void {
    if (!\in_array($this->displayType, ['form', 'view'])) {
      throw new \LogicException(sprintf('Property %s::displayType cannot be empty.', static::class));
    }
  }

  /**
   * Return enabled displays based on the $displayType property.
   *
   * Primarily used so inheritors do not need to define the ::displays method in
   * addition to $displayType.
   *
   * @return \Drupal\Core\Entity\Display\EntityDisplayInterface[]
   */
  protected function displays(): array {
    return match ($this->displayType) {
      'form' => $this->enabledFormDisplays(),
      'view' => $this->enabledViewDisplays(),
      default => [],
    };
  }

  /**
   * Load a specific form display mode for the task's config entity.
   */
  protected function formDisplay(string $mode): EntityFormDisplayInterface {
    return $this->entityDisplayRepository->getFormDisplay(
      $this->entityTypeBundleOf(),
      $this->entityTypeBundle(),
      $mode,
    );
  }

  /**
   * Load all enabled form display modes for the task's config entity.
   *
   * @return \Drupal\Core\Entity\Display\EntityFormDisplayInterface[]
   */
  protected function enabledFormDisplays(): array {
    return array_map($this->formDisplay(...), $this->enabledFormDisplayNames());
  }

  /**
   * Get all enabled form display mode names for the task's config entity.
   *
   * @return string[]
   */
  protected function enabledFormDisplayNames(): array {
    return array_keys($this->entityDisplayRepository->getFormModeOptionsByBundle(
      $this->entityTypeBundleOf(),
      $this->entityTypeBundle(),
    ));
  }

  /**
   * Load a specific view display mode for the task's config entity.
   */
  protected function viewDisplay(string $mode): EntityViewDisplayInterface {
    return $this->entityDisplayRepository->getViewDisplay(
      $this->entityTypeBundleOf(),
      $this->entityTypeBundle(),
      $mode,
    );
  }

  /**
   * Load all enabled view display modes for the task's config entity.
   *
   * @return \Drupal\Core\Entity\Display\EntityViewDisplayInterface[]
   */
  protected function enabledViewDisplays(): array {
    return array_map($this->viewDisplay(...), $this->enabledViewDisplayNames());
  }

  /**
   * Get all enabled view display mode names for the task's config entity.
   *
   * @return string[]
   */
  protected function enabledViewDisplayNames(): array {
    return array_keys($this->entityDisplayRepository->getViewModeOptionsByBundle(
      $this->entityTypeBundleOf(),
      $this->entityTypeBundle(),
    ));
  }

  /**
   * Get the field definition for a field.
   */
  protected function fieldDefinition(string $field): FieldDefinitionInterface|null {
    $definitions = $this->entityFieldManager->getFieldDefinitions(
      $this->entityTypeBundleOf(),
      $this->entityTypeBundle(),
    );
    return $definitions[$field] ?? NULL;
  }

}

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

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