3 namespace Drupal\Core\Field;
5 use Drupal\Core\Action\ActionBase;
6 use Drupal\Core\Session\AccountInterface;
9 * Provides a base for action plugins that update one or more fields.
11 * Example implementation:
14 * class PromoteAndMakeSticky extends FieldUpdateActionBase {
16 * protected function getFieldsToUpdate() {
18 * 'status' => NODE_PROMOTED,
19 * 'sticky' => NODE_STICKY,
26 * @see \Drupal\node\Plugin\Action\PublishNode
28 abstract class FieldUpdateActionBase extends ActionBase {
31 * Gets an array of values to be set.
34 * Array of values with field names as keys.
36 abstract protected function getFieldsToUpdate();
41 public function execute($entity = NULL) {
42 foreach ($this->getFieldsToUpdate() as $field => $value) {
43 $entity->$field = $value;
51 public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
52 /** @var \Drupal\Core\Access\AccessResultInterface $result */
53 $result = $object->access('update', $account, TRUE);
55 foreach ($this->getFieldsToUpdate() as $field => $value) {
56 $result->andIf($object->{$field}->access('edit', $account, TRUE));
59 return $return_as_object ? $result : $result->isAllowed();