NodeSelection.php 1.55 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\node\Plugin\EntityReferenceSelection\NodeSelection.
6 7
 */

8
namespace Drupal\node\Plugin\EntityReferenceSelection;
9

10
use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
11
use Drupal\Core\Form\FormStateInterface;
12 13 14 15

/**
 * Provides specific access control for the node entity type.
 *
16
 * @EntityReferenceSelection(
17
 *   id = "default:node",
18 19 20 21 22 23
 *   label = @Translation("Node selection"),
 *   entity_types = {"node"},
 *   group = "default",
 *   weight = 1
 * )
 */
24
class NodeSelection extends DefaultSelection {
25

26 27 28 29 30 31 32 33 34
  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $form['target_bundles']['#title'] = $this->t('Content types');
    return $form;
  }

35
  /**
36
   * {@inheritdoc}
37
   */
38
  protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
39 40 41 42 43 44
    $query = parent::buildEntityQuery($match, $match_operator);
    // Adding the 'node_access' tag is sadly insufficient for nodes: core
    // requires us to also know about the concept of 'published' and
    // 'unpublished'. We need to do that as long as there are no access control
    // modules in use on the site. As long as one access control module is there,
    // it is supposed to handle this check.
45
    if (!$this->currentUser->hasPermission('bypass node access') && !count($this->moduleHandler->getImplementations('node_grants'))) {
46 47 48 49
      $query->condition('status', NODE_PUBLISHED);
    }
    return $query;
  }
50

51
}