65affd8251d2ad36869fb4da5b777dddedc4653d
[yaffs-website] / web / modules / contrib / media_entity_actions / src / Plugin / Action / PublishMedia.php
1 <?php
2
3 namespace Drupal\media_entity_actions\Plugin\Action;
4
5 use Drupal\Core\Action\ActionBase;
6 use Drupal\Core\Session\AccountInterface;
7 use Drupal\media\MediaInterface;
8
9 /**
10  * Publishes a media item.
11  *
12  * @Action(
13  *   id = "media_publish_action",
14  *   label = @Translation("Publish media"),
15  *   type = "media"
16  * )
17  */
18 class PublishMedia extends ActionBase {
19
20   /**
21    * {@inheritdoc}
22    */
23   public function execute(MediaInterface $entity = NULL) {
24     if ($entity) {
25       $entity->setPublished()->save();
26     }
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
33     /** @var \Drupal\media\MediaInterface $object */
34     $access = $object->access('update', $account, TRUE)
35       ->andIf($object->status->access('update', $account, TRUE));
36
37     return $return_as_object ? $access : $access->isAllowed();
38   }
39
40 }