ArticleRepository.php 2.39 KB
Newer Older
e0ipso's avatar
e0ipso committed
1
2
3
4
<?php

namespace Drupal\typed_entity_example\TypedRepositories;

5
use Drupal\Core\Entity\EntityTypeInterface;
e0ipso's avatar
e0ipso committed
6
use Drupal\Core\Entity\Query\ConditionInterface;
e0ipso's avatar
e0ipso committed
7
use Drupal\typed_entity\TypedRepositories\TypedEntityRepositoryBase;
8
9
use Drupal\typed_entity\WrappedEntityVariants\FieldValueVariantCondition;
use Drupal\typed_entity_example\WrappedEntities\BakingArticle;
e0ipso's avatar
e0ipso committed
10

e0ipso's avatar
e0ipso committed
11
12
13
14
/**
 * The repository for articles.
 */
final class ArticleRepository extends TypedEntityRepositoryBase {
e0ipso's avatar
e0ipso committed
15

e0ipso's avatar
e0ipso committed
16
17
18
19
20
  /**
   * The field that contains the data about the article tags.
   */
  const FIELD_TAGS_NAME = 'field_tags';

e0ipso's avatar
e0ipso committed
21
22
23
  /**
   * {@inheritdoc}
   */
24
25
26
27
28
29
30
31
32
  public function init(EntityTypeInterface $entity_type, string $bundle, string $wrapper_class): void {
    parent::init($entity_type, $bundle, $wrapper_class);
    $field_map = $this->container->get('entity_field.manager')->getFieldMap();
    $has_field = $field_map[$entity_type->id()][static::FIELD_TAGS_NAME]['bundles'][$bundle] ?? NULL;
    if ($has_field) {
      $this->variantConditions = [
        new FieldValueVariantCondition(static::FIELD_TAGS_NAME, 24, BakingArticle::class),
      ];
    }
33
34
  }

e0ipso's avatar
e0ipso committed
35
  /**
e0ipso's avatar
e0ipso committed
36
37
   * Finds article by tags.
   *
e0ipso's avatar
e0ipso committed
38
   * @param string[] $tags
e0ipso's avatar
e0ipso committed
39
   *   The tags to search for.
e0ipso's avatar
e0ipso committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
   *
   * @return \Drupal\typed_entity_example\WrappedEntities\Article[]
   *   The wrapped entities.
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   * @throws \Drupal\typed_entity\InvalidValueException
   */
  public function findByTags(array $tags): array {
    $items = $this->findItemsByTags($tags);
    return $this->wrapMultipleById($items);
  }

  /**
   * Find the entity IDs for the articles tagged with any of the provided tags.
   *
   * @param array $tags
   *   The list of tags.
   *
   * @return array
   *   The result of the execution of the query.
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   */
  private function findItemsByTags(array $tags) {
    $query = $this->getQuery();
    // Find all the articles that have at least one of the tags with insensitive
    // case match.
    $field_path = static::FIELD_TAGS_NAME . '.entity.name';
    $orGroup = array_reduce(
      $tags,
e0ipso's avatar
e0ipso committed
70
71
      function (ConditionInterface $orCondition, string $tag) use ($field_path) {
        return $orCondition->condition($field_path, $tag, 'LIKE');
e0ipso's avatar
e0ipso committed
72
73
74
75
76
77
78
      },
      $query->orConditionGroup()
    );
    return $query
      ->condition($orGroup)
      ->execute();
  }
e0ipso's avatar
e0ipso committed
79
80

}