WorkspacePointerSelection.php in Workspace 8
File
src/Plugin/EntityReferenceSelection/WorkspacePointerSelection.php
View source
<?php
namespace Drupal\workspace\Plugin\EntityReferenceSelection;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
use Drupal\multiversion\Entity\WorkspaceInterface;
class WorkspacePointerSelection extends DefaultSelection {
public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
$query = parent::buildEntityQuery($match, $match_operator);
if (isset($this
->getConfiguration()['entity']) && $this
->getConfiguration()['entity'] instanceof WorkspaceInterface && !$this
->getConfiguration()['entity']
->isNew()) {
$group = $query
->orConditionGroup()
->condition('workspace_pointer', $this
->getConfiguration()['entity']
->id(), '<>')
->notExists('workspace_pointer');
$query
->condition($group);
}
return $query;
}
public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
$target_type = $this
->getConfiguration()['target_type'];
$query = $this
->buildEntityQuery($match, $match_operator);
if ($limit > 0) {
$query
->range(0, $limit);
}
$result = $query
->execute();
if (empty($result)) {
return [];
}
$options = [];
$entities = $this->entityManager
->getStorage($target_type)
->loadMultiple($result);
foreach ($entities as $entity_id => $entity) {
if ($workspace = $entity
->getWorkspace()) {
if (!$workspace
->isPublished() || $workspace
->getQueuedForDelete()) {
continue;
}
}
$bundle = $entity
->bundle();
$options[$bundle][$entity_id] = Html::escape($this->entityManager
->getTranslationFromContext($entity)
->label());
}
return $options;
}
}