Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / linkit / src / Plugin / Linkit / Matcher / NodeMatcher.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\linkit\Plugin\Linkit\Matcher\NodeMatcher.
6  */
7
8 namespace Drupal\linkit\Plugin\Linkit\Matcher;
9
10 use Drupal\Core\Form\FormStateInterface;
11
12 /**
13  * @Matcher(
14  *   id = "entity:node",
15  *   target_entity = "node",
16  *   label = @Translation("Content"),
17  *   provider = "node"
18  * )
19  */
20 class NodeMatcher extends EntityMatcher {
21
22   /**
23    * {@inheritdoc}
24    */
25   public function getSummary() {
26     $summery = parent::getSummary();
27
28     $summery[] = $this->t('Include unpublished: @include_unpublished', [
29       '@include_unpublished' => $this->configuration['include_unpublished'] ? $this->t('Yes') : $this->t('No'),
30     ]);
31
32     return $summery;
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public function defaultConfiguration() {
39     return parent::defaultConfiguration() + [
40       'include_unpublished' => FALSE,
41     ];
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function calculateDependencies() {
48     return parent::calculateDependencies() + [
49       'module' => ['node'],
50     ];
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
57     $form = parent::buildConfigurationForm($form, $form_state);
58
59     $form['include_unpublished'] = [
60       '#title' => t('Include unpublished nodes'),
61       '#type' => 'checkbox',
62       '#default_value' => $this->configuration['include_unpublished'],
63       '#description' => t('In order to see unpublished nodes, the requesting user must also have permissions to do so.'),
64     ];
65
66     return $form;
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
73     parent::submitConfigurationForm($form, $form_state);
74
75     $this->configuration['include_unpublished'] = $form_state->getValue('include_unpublished');
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   protected function buildEntityQuery($match) {
82     $query = parent::buildEntityQuery($match);
83
84     $no_access = !$this->currentUser->hasPermission('bypass node access') && !count($this->moduleHandler->getImplementations('node_grants'));
85     if ($this->configuration['include_unpublished'] !== TRUE || $no_access) {
86       $query->condition('status', NODE_PUBLISHED);
87     }
88
89     return $query;
90   }
91
92 }