TermDelta.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. namespace Drupal\materio_sapi\Plugin\search_api\processor;
  3. use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
  4. use Drupal\Component\Plugin\Exception\PluginNotFoundException;
  5. use Drupal\Core\Cache\Cache;
  6. use Drupal\Core\Cache\CacheBackendInterface;
  7. use Drupal\Core\Entity\ContentEntityTypeInterface;
  8. use Drupal\Core\Entity\EntityFieldManager;
  9. use Drupal\Core\Entity\EntityInterface;
  10. use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
  11. use Drupal\Core\Entity\EntityTypeManagerInterface;
  12. use Drupal\Core\Language\LanguageManagerInterface;
  13. use Drupal\search_api\Datasource\DatasourceInterface;
  14. use Drupal\search_api\IndexInterface;
  15. use Drupal\search_api\Item\ItemInterface;
  16. use Drupal\search_api\Processor\EntityProcessorProperty;
  17. use Drupal\search_api\Processor\ProcessorPluginBase;
  18. use Drupal\search_api\SearchApiException;
  19. use Drupal\search_api\Utility\Utility;
  20. use Symfony\Component\DependencyInjection\ContainerInterface;
  21. /**
  22. * Allows indexing of reverse entity references.
  23. *
  24. * @SearchApiProcessor(
  25. * id = "term_delta",
  26. * label = @Translation("Term Delta"),
  27. * description = @Translation("Allow to boost term reference field value regarding delta."),
  28. * stages = {
  29. * "add_properties" = 0,
  30. * },
  31. * )
  32. */
  33. class TermDelta extends ProcessorPluginBase {
  34. /**
  35. * Static cache for all entity references.
  36. *
  37. * @var array[][]|null
  38. *
  39. * @see \Drupal\search_api\Plugin\search_api\processor\ReverseEntityReferences::getEntityReferences()
  40. */
  41. protected $references;
  42. /**
  43. * The entity type manager.
  44. *
  45. * @var \Drupal\Core\Entity\EntityTypeManagerInterface|null
  46. */
  47. protected $entityTypeManager;
  48. /**
  49. * The entity field manager.
  50. *
  51. * @var \Drupal\Core\Entity\EntityFieldManager|null
  52. */
  53. protected $entityFieldManager;
  54. /**
  55. * The entity type bundle info.
  56. *
  57. * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface|null
  58. */
  59. protected $entityTypeBundleInfo;
  60. /**
  61. * The language manager.
  62. *
  63. * @var \Drupal\Core\Language\LanguageManagerInterface|null
  64. */
  65. protected $languageManager;
  66. /**
  67. * The cache.
  68. *
  69. * @var \Drupal\Core\Cache\CacheBackendInterface|null
  70. */
  71. protected $cache;
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  76. /** @var static $processor */
  77. $processor = parent::create($container, $configuration, $plugin_id, $plugin_definition);
  78. $processor->setEntityTypeManager($container->get('entity_type.manager'));
  79. $processor->setEntityFieldManager($container->get('entity_field.manager'));
  80. $processor->setEntityTypeBundleInfo($container->get('entity_type.bundle.info'));
  81. $processor->setLanguageManager($container->get('language_manager'));
  82. $processor->setCache($container->get('cache.default'));
  83. return $processor;
  84. }
  85. /**
  86. * Retrieves the entity type manager.
  87. *
  88. * @return \Drupal\Core\Entity\EntityTypeManagerInterface
  89. * The entity type manager.
  90. */
  91. public function getEntityTypeManager() {
  92. return $this->entityTypeManager ?: \Drupal::entityTypeManager();
  93. }
  94. /**
  95. * Sets the entity type manager.
  96. *
  97. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  98. * The new entity type manager.
  99. *
  100. * @return $this
  101. */
  102. public function setEntityTypeManager(EntityTypeManagerInterface $entity_type_manager) {
  103. $this->entityTypeManager = $entity_type_manager;
  104. return $this;
  105. }
  106. /**
  107. * Retrieves the entity field manager.
  108. *
  109. * @return \Drupal\Core\Entity\EntityFieldManager
  110. * The entity field manager.
  111. */
  112. public function getEntityFieldManager() {
  113. return $this->entityFieldManager ?: \Drupal::service('entity_field.manager');
  114. }
  115. /**
  116. * Sets the entity field manager.
  117. *
  118. * @param \Drupal\Core\Entity\EntityFieldManager $entity_field_manager
  119. * The new entity field manager.
  120. *
  121. * @return $this
  122. */
  123. public function setEntityFieldManager(EntityFieldManager $entity_field_manager) {
  124. $this->entityFieldManager = $entity_field_manager;
  125. return $this;
  126. }
  127. /**
  128. * Retrieves the entity type bundle info.
  129. *
  130. * @return \Drupal\Core\Entity\EntityTypeBundleInfoInterface
  131. * The entity type bundle info.
  132. */
  133. public function getEntityTypeBundleInfo() {
  134. return $this->entityTypeBundleInfo ?: \Drupal::service('entity_type.bundle.info');
  135. }
  136. /**
  137. * Sets the entity type bundle info.
  138. *
  139. * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
  140. * The new entity type bundle info.
  141. *
  142. * @return $this
  143. */
  144. public function setEntityTypeBundleInfo(EntityTypeBundleInfoInterface $entity_type_bundle_info) {
  145. $this->entityTypeBundleInfo = $entity_type_bundle_info;
  146. return $this;
  147. }
  148. /**
  149. * Retrieves the language manager.
  150. *
  151. * @return \Drupal\Core\Language\LanguageManagerInterface
  152. * The language manager.
  153. */
  154. public function getLanguageManager() {
  155. return $this->languageManager ?: \Drupal::service('language_manager');
  156. }
  157. /**
  158. * Sets the language manager.
  159. *
  160. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
  161. * The new language manager.
  162. *
  163. * @return $this
  164. */
  165. public function setLanguageManager(LanguageManagerInterface $language_manager) {
  166. $this->languageManager = $language_manager;
  167. return $this;
  168. }
  169. /**
  170. * Retrieves the cache.
  171. *
  172. * @return \Drupal\Core\Cache\CacheBackendInterface
  173. * The cache.
  174. */
  175. public function getCache() {
  176. return $this->cache ?: \Drupal::service('cache.default');
  177. }
  178. /**
  179. * Sets the cache.
  180. *
  181. * @param \Drupal\Core\Cache\CacheBackendInterface $cache
  182. * The new cache.
  183. *
  184. * @return $this
  185. */
  186. public function setCache(CacheBackendInterface $cache) {
  187. $this->cache = $cache;
  188. return $this;
  189. }
  190. /**
  191. * {@inheritdoc}
  192. */
  193. public static function supportsIndex(IndexInterface $index) {
  194. foreach ($index->getDatasources() as $datasource) {
  195. if ($datasource->getEntityTypeId()) {
  196. return TRUE;
  197. }
  198. }
  199. return FALSE;
  200. }
  201. /**
  202. * {@inheritdoc}
  203. */
  204. public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
  205. // ksm($datasource);
  206. $properties = [];
  207. if (!$datasource || !$datasource->getEntityTypeId()) {
  208. return $properties;
  209. }
  210. $references = $this->getEntityReferences();
  211. // ksm($references);
  212. // $entity_type_id = $datasource->getEntityTypeId();
  213. // dsm($entity_type_id);
  214. if (isset($references['taxonomy_term'])) {
  215. foreach ($references['taxonomy_term'] as $key => $reference) {
  216. $entity_type_id = $reference['entity_type'];
  217. try {
  218. $entity_type = $this->getEntityTypeManager()
  219. ->getDefinition($entity_type_id);
  220. }
  221. catch (PluginNotFoundException $e) {
  222. continue;
  223. }
  224. // ksm($entity_type);
  225. for ($delta=0; $delta < 5; $delta++) {
  226. $args = [
  227. '@delta' => $delta,
  228. '@entity_type' => $entity_type->getLabel(),
  229. '@property' => $reference['label'],
  230. ];
  231. $definition = [
  232. 'label' => $this->t('@property [@delta] on @entity_type', $args),
  233. // 'description' => $this->t("All %entity_type entities that reference this item via the %property field."),
  234. 'description' => $this->t("Term Delta [@delta] from field @property on @entity_type.", $args),
  235. 'type' => "entity:taxonomy_term",
  236. 'processor_id' => $this->getPluginId(),
  237. // We can't really know whether this will end up being multi-valued, so
  238. // we err on the side of caution.
  239. 'is_list' => TRUE,
  240. ];
  241. $property = new EntityProcessorProperty($definition);
  242. $property->setEntityTypeId('taxonomy_term');
  243. $properties["materio_sapi_term_delta__{$entity_type_id}__{$reference['label']}__{$delta}"] = $property;
  244. // code...
  245. }
  246. }
  247. }
  248. return $properties;
  249. }
  250. /**
  251. * {@inheritdoc}
  252. */
  253. public function addFieldValues(ItemInterface $item) {
  254. // get the original indexed entity
  255. try {
  256. $entity = $item->getOriginalObject()->getValue();
  257. }
  258. catch (SearchApiException $e) {
  259. return;
  260. }
  261. if (!($entity instanceof EntityInterface)) {
  262. return;
  263. }
  264. // get the original
  265. $entity_type_id = $entity->getEntityTypeId();
  266. $entity_id = $entity->id();
  267. $langcode = $entity->language()->getId();
  268. $datasource_id = $item->getDatasourceId();
  269. /** @var \Drupal\search_api\Item\FieldInterface[][][] $to_extract */
  270. $to_extract = [];
  271. $prefix = 'materio_sapi_term_delta__';
  272. $prefix_length = strlen($prefix);
  273. foreach ($item->getFields() as $field) {
  274. $property_path = $field->getPropertyPath();
  275. list($direct, $nested) = Utility::splitPropertyPath($property_path, FALSE);
  276. if ($field->getDatasourceId() === $datasource_id
  277. && substr($direct, 0, $prefix_length) === $prefix) {
  278. $property_name = substr($direct, $prefix_length);
  279. $to_extract[$property_name][$nested][] = $field;
  280. }
  281. }
  282. // ksm($to_extract);
  283. $references = $this->getEntityReferences();
  284. foreach ($to_extract as $property_name => $fields_to_extract) {
  285. if (!isset($references[$entity_type_id][$property_name])) {
  286. continue;
  287. }
  288. $property_info = $references[$entity_type_id][$property_name];
  289. try {
  290. $storage = $this->getEntityTypeManager()
  291. ->getStorage($property_info['entity_type']);
  292. }
  293. // @todo Replace with multi-catch once we depend on PHP 7.1+.
  294. catch (InvalidPluginDefinitionException $e) {
  295. continue;
  296. }
  297. catch (PluginNotFoundException $e) {
  298. continue;
  299. }
  300. $entity_ids = $storage->getQuery()
  301. ->accessCheck(FALSE)
  302. ->condition($property_info['property'], $entity_id)
  303. ->execute();
  304. $entities = $storage->loadMultiple($entity_ids);
  305. if (!$entities) {
  306. continue;
  307. }
  308. // This is a pretty hack-y work-around to make property extraction work for
  309. // Views fields, too. In general, adding entities as field values is a
  310. // pretty bad idea, so this might blow up in some use cases. Just do it for
  311. // now and hope for the best.
  312. if (isset($fields_to_extract[''])) {
  313. foreach ($fields_to_extract[''] as $field) {
  314. $field->setValues(array_values($entities));
  315. }
  316. unset($fields_to_extract['']);
  317. }
  318. foreach ($entities as $referencing_entity) {
  319. $typed_data = $referencing_entity->getTypedData();
  320. $this->getFieldsHelper()
  321. ->extractFields($typed_data, $fields_to_extract, $langcode);
  322. }
  323. }
  324. }
  325. /**
  326. * Collects all entity references.
  327. *
  328. * @return array[][]
  329. * An associative array of entity reference information keyed by the
  330. * referenced entity type's ID and a custom identifier for the property
  331. * (consisting of referencing entity type and property name), with values
  332. * being associative arrays with the following keys:
  333. * - label: The property label.
  334. * - entity_type: The referencing entity type.
  335. * - property: The property name.
  336. */
  337. public function getEntityReferences() {
  338. if ($this->references !== NULL) {
  339. return $this->references;
  340. }
  341. // Property labels differ by language, so we need to vary the cache
  342. // according to the current language.
  343. $langcode = $this->getLanguageManager()->getCurrentLanguage()->getId();
  344. $cid = "search_api:term_delta:$langcode";
  345. $cache = $this->getCache()->get($cid);
  346. if (isset($cache->data)) {
  347. $this->references = $cache->data;
  348. }
  349. else {
  350. $this->references = [];
  351. $entity_types = $this->getEntityTypeManager()->getDefinitions();
  352. $field_manager = $this->getEntityFieldManager();
  353. $entity_type_bundle_info = $this->getEntityTypeBundleInfo();
  354. foreach ($entity_types as $entity_type_id => $entity_type) {
  355. if (!($entity_type instanceof ContentEntityTypeInterface)) {
  356. continue;
  357. }
  358. /** @var \Drupal\Core\Field\FieldDefinitionInterface[] $properties */
  359. $properties = $field_manager->getBaseFieldDefinitions($entity_type_id);
  360. $bundles = $entity_type_bundle_info->getBundleInfo($entity_type_id);
  361. foreach ($bundles as $bundle => $info) {
  362. $properties += $field_manager->getFieldDefinitions($entity_type_id, $bundle);
  363. }
  364. foreach ($properties as $name => $property) {
  365. if ($property->getType() !== 'entity_reference') {
  366. continue;
  367. }
  368. $settings = $property->getSettings();
  369. if (empty($settings['target_type'])) {
  370. continue;
  371. }
  372. $this->references[$settings['target_type']]["{$entity_type_id}__$name"] = [
  373. 'label' => $property->getLabel(),
  374. 'entity_type' => $entity_type_id,
  375. 'property' => $name,
  376. ];
  377. }
  378. }
  379. $tags = [
  380. 'entity_types',
  381. 'entity_bundles',
  382. 'entity_field_info',
  383. ];
  384. $this->getCache()->set($cid, $this->references, Cache::PERMANENT, $tags);
  385. }
  386. return $this->references;
  387. }
  388. }