comparison core/modules/media/src/Plugin/EntityReferenceSelection/MediaSelection.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
1 <?php
2
3 namespace Drupal\media\Plugin\EntityReferenceSelection;
4
5 use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
6
7 /**
8 * Provides specific access control for the media entity type.
9 *
10 * @EntityReferenceSelection(
11 * id = "default:media",
12 * label = @Translation("Media selection"),
13 * entity_types = {"media"},
14 * group = "default",
15 * weight = 1
16 * )
17 */
18 class MediaSelection extends DefaultSelection {
19
20 /**
21 * {@inheritdoc}
22 */
23 protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
24 $query = parent::buildEntityQuery($match, $match_operator);
25
26 // Ensure that users with insufficient permission cannot see unpublished
27 // entities.
28 if (!$this->currentUser->hasPermission('administer media')) {
29 $query->condition('status', 1);
30 }
31 return $query;
32 }
33
34 /**
35 * {@inheritdoc}
36 */
37 public function createNewEntity($entity_type_id, $bundle, $label, $uid) {
38 $media = parent::createNewEntity($entity_type_id, $bundle, $label, $uid);
39
40 // In order to create a referenceable media, it needs to published.
41 /** @var \Drupal\media\MediaInterface $media */
42 $media->setPublished();
43
44 return $media;
45 }
46
47 /**
48 * {@inheritdoc}
49 */
50 public function validateReferenceableNewEntities(array $entities) {
51 $entities = parent::validateReferenceableNewEntities($entities);
52 // Mirror the conditions checked in buildEntityQuery().
53 if (!$this->currentUser->hasPermission('administer media')) {
54 $entities = array_filter($entities, function ($media) {
55 /** @var \Drupal\media\MediaInterface $media */
56 return $media->isPublished();
57 });
58 }
59 return $entities;
60 }
61
62 }