Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / taxonomy / src / Plugin / EntityReferenceSelection / TermSelection.php
1 <?php
2
3 namespace Drupal\taxonomy\Plugin\EntityReferenceSelection;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\taxonomy\Entity\Vocabulary;
9
10 /**
11  * Provides specific access control for the taxonomy_term entity type.
12  *
13  * @EntityReferenceSelection(
14  *   id = "default:taxonomy_term",
15  *   label = @Translation("Taxonomy Term selection"),
16  *   entity_types = {"taxonomy_term"},
17  *   group = "default",
18  *   weight = 1
19  * )
20  */
21 class TermSelection extends DefaultSelection {
22
23   /**
24    * {@inheritdoc}
25    */
26   public function defaultConfiguration() {
27     return [
28       'sort' => [
29         'field' => 'name',
30         'direction' => 'asc',
31       ],
32     ] + parent::defaultConfiguration();
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
39     $form = parent::buildConfigurationForm($form, $form_state);
40
41     // Sorting is not possible for taxonomy terms because we use
42     // \Drupal\taxonomy\TermStorageInterface::loadTree() to retrieve matches.
43     $form['sort']['#access'] = FALSE;
44
45     return $form;
46
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
53     if ($match || $limit) {
54       return parent::getReferenceableEntities($match, $match_operator, $limit);
55     }
56
57     $options = [];
58
59     $bundles = $this->entityManager->getBundleInfo('taxonomy_term');
60     $bundle_names = $this->getConfiguration()['target_bundles'] ?: array_keys($bundles);
61
62     $has_admin_access = $this->currentUser->hasPermission('administer taxonomy');
63     $unpublished_terms = [];
64     foreach ($bundle_names as $bundle) {
65       if ($vocabulary = Vocabulary::load($bundle)) {
66         /** @var \Drupal\taxonomy\TermInterface[] $terms */
67         if ($terms = $this->entityManager->getStorage('taxonomy_term')->loadTree($vocabulary->id(), 0, NULL, TRUE)) {
68           foreach ($terms as $term) {
69             if (!$has_admin_access && (!$term->isPublished() || in_array($term->parent->target_id, $unpublished_terms))) {
70               $unpublished_terms[] = $term->id();
71               continue;
72             }
73             $options[$vocabulary->id()][$term->id()] = str_repeat('-', $term->depth) . Html::escape($this->entityManager->getTranslationFromContext($term)->label());
74           }
75         }
76       }
77     }
78
79     return $options;
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function countReferenceableEntities($match = NULL, $match_operator = 'CONTAINS') {
86     if ($match) {
87       return parent::countReferenceableEntities($match, $match_operator);
88     }
89
90     $total = 0;
91     $referenceable_entities = $this->getReferenceableEntities($match, $match_operator, 0);
92     foreach ($referenceable_entities as $bundle => $entities) {
93       $total += count($entities);
94     }
95     return $total;
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
102     $query = parent::buildEntityQuery($match, $match_operator);
103
104     // Adding the 'taxonomy_term_access' tag is sadly insufficient for terms:
105     // core requires us to also know about the concept of 'published' and
106     // 'unpublished'.
107     if (!$this->currentUser->hasPermission('administer taxonomy')) {
108       $query->condition('status', 1);
109     }
110     return $query;
111   }
112
113   /**
114    * {@inheritdoc}
115    */
116   public function createNewEntity($entity_type_id, $bundle, $label, $uid) {
117     $term = parent::createNewEntity($entity_type_id, $bundle, $label, $uid);
118
119     // In order to create a referenceable term, it needs to published.
120     /** @var \Drupal\taxonomy\TermInterface $term */
121     $term->setPublished();
122
123     return $term;
124   }
125
126   /**
127    * {@inheritdoc}
128    */
129   public function validateReferenceableNewEntities(array $entities) {
130     $entities = parent::validateReferenceableNewEntities($entities);
131     // Mirror the conditions checked in buildEntityQuery().
132     if (!$this->currentUser->hasPermission('administer taxonomy')) {
133       $entities = array_filter($entities, function ($term) {
134         /** @var \Drupal\taxonomy\TermInterface $term */
135         return $term->isPublished();
136       });
137     }
138     return $entities;
139   }
140
141 }