SetCustomPublishOptionValue.php in Custom Publishing Options 8
File
src/Plugin/Action/SetCustomPublishOptionValue.php
View source
<?php
namespace Drupal\custom_pub\Plugin\Action;
use Drupal\Core\Action\ConfigurableActionBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
class SetCustomPublishOptionValue extends ConfigurableActionBase {
public function execute($node = NULL) {
$option = $this->configuration['option'];
$value = $this->configuration['value'];
$node->{$option} = (bool) $value;
$node
->save();
}
public function defaultConfiguration() {
return [
'option' => NULL,
'value' => NULL,
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$options = [
'' => ' - Select -',
];
$entities = \Drupal::entityTypeManager()
->getStorage('custom_publishing_option')
->loadMultiple();
foreach ($entities as $option) {
$options[$option
->id()] = $option
->label();
}
$form['option'] = [
'#title' => $this
->t('Custom Publishing Options'),
'#type' => 'select',
'#options' => $options,
'#required' => TRUE,
'#description' => $this
->t('The custom publishing option to use.'),
'#default_value' => $this->configuration['option'],
];
$form['value'] = [
'#title' => $this
->t('Leave unchecked for FALSE'),
'#type' => 'checkbox',
'#description' => $this
->t('The value you want to set the option to.'),
'#default_value' => $this->configuration['value'],
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['option'] = $form_state
->getValue('option');
$this->configuration['value'] = (bool) $form_state
->getValue('value');
}
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
$access = $object
->access('update', $account, TRUE)
->andIf($object->status
->access('edit', $account, TRUE));
return $return_as_object ? $access : $access
->isAllowed();
}
}