1+ <?php
2+
3+ namespace Drupal \pantheon_content_publisher \Plugin \EntityReferenceSelection ;
4+
5+ use Drupal \Core \Entity \Plugin \EntityReferenceSelection \DefaultSelection ;
6+
7+ /**
8+ * Provides a selection plugin for Pantheon documents.
9+ *
10+ * @EntityReferenceSelection(
11+ * id = "pantheon_document_selection",
12+ * label = @Translation("Pantheon Document Selection"),
13+ * group = "default",
14+ * weight = 0
15+ * )
16+ */
17+ class PantheonDocumentSelection extends DefaultSelection {
18+
19+ /**
20+ * {@inheritdoc}
21+ */
22+ protected function buildEntityQuery ($ match = NULL , $ match_operator = 'CONTAINS ' ): QueryInterface {
23+ $ configuration = $ this ->getConfiguration ();
24+ $ target_type = 'pantheon_document ' ;
25+ $ entity_type = $ this ->entityTypeManager ->getDefinition ($ target_type );
26+
27+ /** @var \Drupal\Core\Entity\Query\QueryInterface $query */
28+ $ query = $ this ->entityTypeManager ->getStorage ($ target_type )->getQuery ();
29+ $ query ->accessCheck (TRUE );
30+
31+ if (isset ($ match ) && $ label_key = $ entity_type ->getKey ('label ' )) {
32+ $ query ->condition ($ label_key , $ match , $ match_operator );
33+ }
34+
35+ $ collection_ids = $ configuration ['target_bundles ' ] ?? [];
36+ if (!empty ($ collection_ids )) {
37+ $ query ->condition ('collection ' , $ collection_ids , 'IN ' );
38+ }
39+
40+ // Add entity-access tag.
41+ $ query ->addTag ($ target_type . '_access ' );
42+ $ query ->addTag ('entity_reference ' );
43+ $ query ->addMetaData ('entity_reference_selection_handler ' , $ this );
44+
45+ // Add the sort option.
46+ if (!empty ($ configuration ['sort ' ]) && $ configuration ['sort ' ]['field ' ] !== '_none ' ) {
47+ $ query ->sort ($ configuration ['sort ' ]['field ' ], $ configuration ['sort ' ]['direction ' ]);
48+ }
49+
50+ return $ query ;
51+ }
52+
53+ /**
54+ * {@inheritdoc}
55+ */
56+ public function validateReferenceableEntities (array $ ids ): array {
57+ return parent ::validateReferenceableEntities ($ ids );
58+ }
59+
60+ }
0 commit comments