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), ]; } } /** * Finds article by tags. * * @param string[] $tags * The tags to search for. * * @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, function (ConditionInterface $orCondition, string $tag) use ($field_path) { return $orCondition->condition($field_path, $tag, 'LIKE'); }, $query->orConditionGroup() ); return $query ->condition($orGroup) ->execute(); } }