1

How do I alter this select list programmatically in my module?

Remove Request publish for this role programmatically

Permissions won't work here since all the roles can use the 'Request publish' transition, but the roles that can also use the 'Published' transition don't need to see 'Request publish'. I think the easiest way to make this change is to remove 'Request publish' from the dropdown list.

I'm working with hook_field_widget_form_alter()

The code that I'm trying to override is from formElement() in ModerationStateWidget.php.

Thanks.

1
  • I think hook_form_alter() will do.
    – No Sssweat
    Jul 6, 2018 at 1:52

1 Answer 1

1

Thanks @No Sssweat. You put me on the right track.

Since this is the node form, I opted to use hook_form_BASE_FORM_ID_alter().

/**
 * Implements hook_form_BASE_FORM_ID_alter() for node_form().
 */
function MY_MODULE_form_node_form_alter(&$form, FormStateInterface $form_state) {
  $roles = \Drupal::currentUser()->getRoles();
  if (array_intersect(['role_1', 'role_2', 'role_3'], $roles)) {
    $options = isset($form['moderation_state']['widget']) ? $form['moderation_state']['widget'][0]['state']['#options'] : [];
    $options = array_diff_key($options, [
      'request_publish' => 'Request Publish',
      'request_unpublish' => 'Request unpublish',
      'request_delete' => 'Request Delete'
    ]);
    $form['moderation_state']['widget'][0]['state']['#options'] = $options;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.