81 lines
2 KiB
Twig
81 lines
2 KiB
Twig
<?php
|
|
|
|
namespace Drupal\{{ machine_name }}\Plugin\Action;
|
|
|
|
{% if configurable %}
|
|
use Drupal\Core\Action\ConfigurableActionBase;
|
|
use Drupal\Core\Form\FormStateInterface;
|
|
use Drupal\Core\Session\AccountInterface;
|
|
{% else %}
|
|
use Drupal\Core\Action\ActionBase;
|
|
use Drupal\Core\Session\AccountInterface;
|
|
{% endif %}
|
|
|
|
/**
|
|
* Provides a {{ plugin_label|article }} action.
|
|
*
|
|
* @Action(
|
|
* id = "{{ plugin_id }}",
|
|
* label = @Translation("{{ plugin_label }}"),
|
|
* type = "node",
|
|
* category = @Translation("{{ category }}")
|
|
* )
|
|
*
|
|
* @DCG
|
|
* For simple updating entity fields consider extending FieldUpdateActionBase.
|
|
*/
|
|
class {{ class }} extends {{ configurable ? 'ConfigurableActionBase' : 'ActionBase' }} {
|
|
|
|
{% if configurable %}
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function defaultConfiguration() {
|
|
return ['title' => ''];
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
|
|
$form['title'] = [
|
|
'#title' => t('New title'),
|
|
'#type' => 'textfield',
|
|
'#required' => TRUE,
|
|
'#default_value' => $this->configuration['title'],
|
|
];
|
|
return $form;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
|
|
$this->configuration['title'] = $form_state->getValue('title');
|
|
}
|
|
|
|
{% endif %}
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function access($node, AccountInterface $account = NULL, $return_as_object = FALSE) {
|
|
/** @var \Drupal\node\NodeInterface $node */
|
|
$access = $node->access('update', $account, TRUE)
|
|
->andIf($node->title->access('edit', $account, TRUE));
|
|
return $return_as_object ? $access : $access->isAllowed();
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function execute($node = NULL) {
|
|
/** @var \Drupal\node\NodeInterface $node */
|
|
{% if configurable %}
|
|
$node->setTitle($this->configuration['title'])->save();
|
|
{% else %}
|
|
$node->setTitle(t('New title'))->save();
|
|
{% endif %}
|
|
}
|
|
|
|
}
|