Commit 18cffdcd authored by catch's avatar catch

Issue #2805993 by blazey, tom_ek, alexpott, Wim Leers, Lund, Virbius: Provide...

Issue #2805993 by blazey, tom_ek, alexpott, Wim Leers, Lund, Virbius: Provide base class for actions that update a field on an entity
parent c3f81d18
<?php
namespace Drupal\Core\Field;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Session\AccountInterface;
/**
* Provides a base for action plugins that update one or more fields.
*
* Example implementation:
*
* @code
* class PromoteAndMakeSticky extends FieldUpdateActionBase {
*
* protected function getFieldsToUpdate() {
* return [
* 'status' => NODE_PROMOTED,
* 'sticky' => NODE_STICKY,
* ];
* }
*
* }
* @endcode
*
* @see \Drupal\node\Plugin\Action\PublishNode
*/
abstract class FieldUpdateActionBase extends ActionBase {
/**
* Gets an array of values to be set.
*
* @return array
* Array of values with field names as keys.
*/
protected abstract function getFieldsToUpdate();
/**
* {@inheritdoc}
*/
public function execute($entity = NULL) {
foreach ($this->getFieldsToUpdate() as $field => $value) {
$entity->$field = $value;
}
$entity->save();
}
/**
* {@inheritdoc}
*/
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
/** @var \Drupal\Core\Access\AccessResultInterface $result */
$result = $object->access('update', $account, TRUE);
foreach ($this->getFieldsToUpdate() as $field => $value) {
$result->andIf($object->{$field}->access('edit', $account, TRUE));
}
return $return_as_object ? $result : $result->isAllowed();
}
}
......@@ -2,8 +2,8 @@
namespace Drupal\node\Plugin\Action;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Field\FieldUpdateActionBase;
use Drupal\node\NodeInterface;
/**
* Demotes a node.
......@@ -14,25 +14,13 @@
* type = "node"
* )
*/
class DemoteNode extends ActionBase {
class DemoteNode extends FieldUpdateActionBase {
/**
* {@inheritdoc}
*/
public function execute($entity = NULL) {
$entity->setPromoted(FALSE);
$entity->save();
}
/**
* {@inheritdoc}
*/
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
/** @var \Drupal\node\NodeInterface $object */
$result = $object->access('update', $account, TRUE)
->andIf($object->promote->access('edit', $account, TRUE));
return $return_as_object ? $result : $result->isAllowed();
protected function getFieldsToUpdate() {
return ['promote' => NodeInterface::NOT_PROMOTED];
}
}
......@@ -2,8 +2,8 @@
namespace Drupal\node\Plugin\Action;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Field\FieldUpdateActionBase;
use Drupal\node\NodeInterface;
/**
* Promotes a node.
......@@ -14,24 +14,13 @@
* type = "node"
* )
*/
class PromoteNode extends ActionBase {
class PromoteNode extends FieldUpdateActionBase {
/**
* {@inheritdoc}
*/
public function execute($entity = NULL) {
$entity->setPromoted(TRUE);
$entity->save();
}
/**
* {@inheritdoc}
*/
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
/** @var \Drupal\node\NodeInterface $object */
$access = $object->access('update', $account, TRUE)
->andif($object->promote->access('edit', $account, TRUE));
return $return_as_object ? $access : $access->isAllowed();
protected function getFieldsToUpdate() {
return ['promote' => NodeInterface::PROMOTED];
}
}
......@@ -2,8 +2,8 @@
namespace Drupal\node\Plugin\Action;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Field\FieldUpdateActionBase;
use Drupal\node\NodeInterface;
/**
* Publishes a node.
......@@ -14,24 +14,13 @@
* type = "node"
* )
*/
class PublishNode extends ActionBase {
class PublishNode extends FieldUpdateActionBase {
/**
* {@inheritdoc}
*/
public function execute($entity = NULL) {
$entity->setPublished()->save();
}
/**
* {@inheritdoc}
*/
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
/** @var \Drupal\node\NodeInterface $object */
$result = $object->access('update', $account, TRUE)
->andIf($object->status->access('edit', $account, TRUE));
return $return_as_object ? $result : $result->isAllowed();
protected function getFieldsToUpdate() {
return ['status' => NodeInterface::PUBLISHED];
}
}
......@@ -2,8 +2,8 @@
namespace Drupal\node\Plugin\Action;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Field\FieldUpdateActionBase;
use Drupal\node\NodeInterface;
/**
* Makes a node sticky.
......@@ -14,23 +14,13 @@
* type = "node"
* )
*/
class StickyNode extends ActionBase {
class StickyNode extends FieldUpdateActionBase {
/**
* {@inheritdoc}
*/
public function execute($entity = NULL) {
$entity->setSticky(TRUE)->save();
}
/**
* {@inheritdoc}
*/
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
/** @var \Drupal\node\NodeInterface $object */
$access = $object->access('update', $account, TRUE)
->andif($object->sticky->access('edit', $account, TRUE));
return $return_as_object ? $access : $access->isAllowed();
protected function getFieldsToUpdate() {
return ['sticky' => NodeInterface::STICKY];
}
}
......@@ -2,8 +2,8 @@
namespace Drupal\node\Plugin\Action;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Field\FieldUpdateActionBase;
use Drupal\node\NodeInterface;
/**
* Unpublishes a node.
......@@ -14,24 +14,13 @@
* type = "node"
* )
*/
class UnpublishNode extends ActionBase {
class UnpublishNode extends FieldUpdateActionBase {
/**
* {@inheritdoc}
*/
public function execute($entity = NULL) {
$entity->setUnpublished()->save();
}
/**
* {@inheritdoc}
*/
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
/** @var \Drupal\node\NodeInterface $object */
$access = $object->access('update', $account, TRUE)
->andIf($object->status->access('edit', $account, TRUE));
return $return_as_object ? $access : $access->isAllowed();
protected function getFieldsToUpdate() {
return ['status' => NodeInterface::NOT_PUBLISHED];
}
}
......@@ -2,8 +2,8 @@
namespace Drupal\node\Plugin\Action;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Field\FieldUpdateActionBase;
use Drupal\node\NodeInterface;
/**
* Makes a node not sticky.
......@@ -14,24 +14,13 @@
* type = "node"
* )
*/
class UnstickyNode extends ActionBase {
class UnstickyNode extends FieldUpdateActionBase {
/**
* {@inheritdoc}
*/
public function execute($entity = NULL) {
$entity->setSticky(FALSE)->save();
}
/**
* {@inheritdoc}
*/
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
/** @var \Drupal\node\NodeInterface $object */
$access = $object->access('update', $account, TRUE)
->andIf($object->sticky->access('edit', $account, TRUE));
return $return_as_object ? $access : $access->isAllowed();
protected function getFieldsToUpdate() {
return ['sticky' => NodeInterface::NOT_STICKY];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment