You are here

public static function ModerationStateWidget::processActions in Workbench Moderation 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/ModerationStateWidget.php \Drupal\workbench_moderation\Plugin\Field\FieldWidget\ModerationStateWidget::processActions()

Process callback to alter action buttons.

File

src/Plugin/Field/FieldWidget/ModerationStateWidget.php, line 202

Class

ModerationStateWidget
Plugin implementation of the 'moderation_state_default' widget.

Namespace

Drupal\workbench_moderation\Plugin\Field\FieldWidget

Code

public static function processActions($element, FormStateInterface $form_state, array &$form) {

  // We'll steal most of the button configuration from the
  // default submit button.
  // However, NodeForm also hides that button for admins (as it adds its own,
  // too), so we have to restore it.
  $default_button = $form['actions']['submit'];
  $default_button['#access'] = TRUE;

  // Add a custom button for each transition we're allowing. The #dropbutton
  // property tells FAPI to cluster them all together into a single widget.
  $options = $element['#options'];
  $entity = $form_state
    ->getFormObject()
    ->getEntity();
  $translatable = !$entity
    ->isNew() && $entity
    ->isTranslatable();
  foreach ($options as $id => $label) {
    $button = [
      '#dropbutton' => 'save',
      '#moderation_state' => $id,
      '#weight' => -10,
    ];
    $button['#value'] = $translatable ? t('Save and @transition (this translation)', [
      '@transition' => $label,
    ]) : t('Save and @transition', [
      '@transition' => $label,
    ]);
    $form['actions']['moderation_state_' . $id] = $button + $default_button;
  }

  // Hide the default buttons, including the specialty ones added by
  // NodeForm.
  foreach ([
    'publish',
    'unpublish',
    'submit',
  ] as $key) {
    $form['actions'][$key]['#access'] = FALSE;
    unset($form['actions'][$key]['#dropbutton']);
  }

  // Setup a callback to translate the button selection back into field
  // widget, so that it will get saved properly.
  $form['#entity_builders']['update_moderation_state'] = [
    get_called_class(),
    'updateStatus',
  ];
  return $element;
}