Skip to content

Commit d776040

Browse files
committed
Fix issue with adding pantheon document reference field
1 parent 213ab22 commit d776040

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}

src/Query/Query.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,33 @@ public function __construct(EntityTypeInterface $entity_type, $conjunction, arra
2323
public function execute() {
2424
$conditions = &$this->condition->conditions();
2525
$collections = NULL;
26+
$target_ids = NULL;
2627
foreach ($conditions as $key => $condition) {
27-
if ($condition['field'] === 'collection' && in_array(($condition['operator'] ?? '='), ['=', 'IN'])) {
28+
if ($condition['field'] === 'collection' && in_array($condition['operator'] ?? '=', ['=', 'IN'])) {
2829
$collections = (array) $condition['value'];
2930
unset($conditions[$key]);
30-
break;
31+
}
32+
33+
if ($condition['field'] === 'id' && in_array($condition['operator'] ?? '=', ['=', 'IN'])) {
34+
$target_ids = (array) $condition['value'];
35+
unset($conditions[$key]);
3136
}
3237
}
38+
3339
$collections = PantheonDocumentCollection::loadMultiple($collections);
3440
$records = [];
3541
foreach ($collections as $collection) {
3642
foreach ($collection->getGraphQL()->getArticles() as $pantheon_record) {
3743
$key = PantheonDocumentStorage::getEntityId($collection, $pantheon_record['id']);
44+
45+
if ($target_ids !== NULL && !in_array($key, $target_ids)) {
46+
continue;
47+
}
48+
3849
$records[$key] = $this->converter->convert($pantheon_record, $collection->id());
3950
}
4051
}
4152

42-
// Copy-paste from
4353
// Drupal\Core\Entity\KeyValueStore\Query\Query::execute().
4454
$result = $this->condition->compile($records);
4555

@@ -68,3 +78,4 @@ public function execute() {
6878
}
6979

7080
}
81+

0 commit comments

Comments
 (0)