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

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

declare(strict_types=1);

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

use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\reviewer\Reviewer\Task\TaskBase;
use Drupal\reviewer_test_kit\Plugin\reviewer\Task\NestedConfigTrait;

/**
 * Base task for checking entity type 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.
 *
 * If the config entity type set on the review does not exist, then a
 * \LogicException will be thrown.
 *
 * Example of a task which checks if a pathauto alias is set for the checked
 * config entity:
 *
 * @code
 * #[Task(
 *   id: 'has_pathauto_alias',
 *   module_dependencies: ['pathauto'],
 * )]
 * final class HasPathautoAlias extends EntityTypeTaskBase {
 *
 *   public function check(): ResultInterface {
 *     $has_pattern = FALSE;
 *     foreach ($this->patterns() as $pattern) {
 *       foreach ($pattern->getSelectionConditions()->getConfiguration() as $configuration) {
 *         if (
 *           \in_array($this->entityTypeBundle(), $configuration['bundles'])
 *           && $pattern->status()
 *         ) {
 *           $has_pattern = TRUE;
 *           break;
 *         }
 *       }
 *     }
 *
 *     return $this->createCheckResult(
 *       $has_pattern,
 *       "{$this->entityTypeFullId()} has a pathauto alias.",
 *       "{$this->entityTypeFullId()} does not have a pathauto alias.",
 *     );
 *   }
 *
 *   private function patterns(): array {
 *     return $this
 *       ->entityTypeManager
 *       ->getStorage('pathauto_pattern')
 *       ->loadByProperties(['type' => "canonical_entities:{$this->entityTypeBundleOf()}"]);
 *   }
 *
 * }
 * @endcode
 *
 * @see \Drupal\Core\Entity\EntityTypeInterface
 */
abstract class EntityTypeTaskBase extends TaskBase {

  use NestedConfigTrait;

  /**
   * The entity type of the configuration entity.
   *
   * @throws \LogicException
   *   When called on a review that does not define a config entity type or with
   *   a config entity type that does not exist.
   */
  protected function entityType(): EntityTypeInterface {
    return $this->getConfigEntity()?->getEntityType() ?? throw new \LogicException('Entity type not defined.');
  }

  /**
   * The ID of the type of the configuration entity.
   *
   * Example: 'node_type' or 'taxonomy_vocabulary'.
   *
   * @throws \LogicException
   *   When called on a review that does not define a config entity type or with
   *   a config entity type that does not exist.
   */
  protected function entityTypeId(): string {
    return $this->entityType()->id();
  }

  /**
   * The bundle of the configuration entity.
   *
   * Example: 'page' or 'news'.
   *
   * @throws \LogicException
   *   When called on a review that does not define a config entity type or with
   *   a config entity type that does not exist.
   */
  protected function entityTypeBundle(): string {
    $bundle = $this->getConfigEntity()?->id() ?? throw new \LogicException('Entity type not defined.');
    return (string) $bundle;
  }

  /**
   * The bundle of the configuration entity.
   *
   * Example: 'node' or 'taxonomy_term'.
   *
   * @throws \LogicException
   *   When called on a review that does not define a config entity type or with
   *   a config entity type that does not exist.
   */
  protected function entityTypeBundleOf(): string {
    return $this->entityType()->getBundleOf() ?? '';
  }

  /**
   * The full ID of the configuration entity.
   *
   * Example 'node.page' or 'taxonomy_term.tag'.
   *
   * @throws \LogicException
   *   When called on a review that does not define a config entity type or with
   *   a config entity type that does not exist.
   */
  protected function entityTypeFullId(): string {
    return implode('.', array_unique([$this->entityTypeBundleOf(), $this->entityTypeBundle()]));
  }

}

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

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