3 namespace Drupal\Core\Action\Plugin\Action;
5 use Drupal\Component\Datetime\TimeInterface;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Drupal\Core\Session\AccountInterface;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
11 * Provides an action that can save any entity.
14 * id = "entity:save_action",
15 * action_label = @Translation("Save"),
16 * deriver = "Drupal\Core\Action\Plugin\Action\Derivative\EntityChangedActionDeriver",
19 class SaveAction extends EntityActionBase {
24 * @var \Drupal\Component\Datetime\TimeInterface
29 * Constructs a SaveAction object.
31 * @param mixed[] $configuration
32 * A configuration array containing information about the plugin instance.
33 * @param string $plugin_id
34 * The plugin ID for the plugin instance.
35 * @param mixed $plugin_definition
36 * The plugin implementation definition.
37 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
38 * The entity type manager.
39 * @param \Drupal\Component\Datetime\TimeInterface $time
42 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, TimeInterface $time) {
43 parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager);
50 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
55 $container->get('entity_type.manager'),
56 $container->get('datetime.time')
63 public function execute($entity = NULL) {
64 $entity->setChangedTime($this->time->getRequestTime())->save();
70 public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
71 // It's not necessary to check the changed field access here, because
72 // Drupal\Core\Field\ChangedFieldItemList would anyway return 'not allowed'.
73 // Also changing the changed field value is only a workaround to trigger an
74 // entity resave. Without a field change, this would not be possible.
75 /** @var \Drupal\Core\Entity\EntityInterface $object */
76 return $object->access('update', $account, $return_as_object);