You are here

public function ContentModerationNotificationsFormBase::buildForm in Content Moderation Notifications 8

Same name and namespace in other branches
  1. 8.3 src/Form/ContentModerationNotificationsFormBase.php \Drupal\content_moderation_notifications\Form\ContentModerationNotificationsFormBase::buildForm()
  2. 8.2 src/Form/ContentModerationNotificationsFormBase.php \Drupal\content_moderation_notifications\Form\ContentModerationNotificationsFormBase::buildForm()

Overrides Drupal\Core\Entity\EntityFormController::form().

Builds the entity add/edit form.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: An associative array containing the current state of the form.

Return value

array An associative array containing the content_moderation_notification add/edit form.

Overrides EntityForm::buildForm

File

src/Form/ContentModerationNotificationsFormBase.php, line 86

Class

ContentModerationNotificationsFormBase
Class ContentModerationNotificationFormBase.

Namespace

Drupal\content_moderation_notifications\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Get anything we need from the base class.
  $form = parent::buildForm($form, $form_state);

  // Drupal provides the entity to us as a class variable. If this is an
  // existing entity, it will be populated with existing values as class
  // variables. If this is a new entity, it will be a new object with the
  // class of our entity. Drupal knows which class to call from the
  // annotation on our ContentModerationNotification class.
  $content_moderation_notification = $this->entity;

  // Retrieve a list of all possible workflow states.
  $states = $this->entityTypeManager
    ->getStorage('moderation_state')
    ->loadMultiple();
  $form['id'] = array(
    '#type' => 'machine_name',
    '#title' => $this
      ->t('Machine name'),
    '#default_value' => $content_moderation_notification
      ->id(),
    '#machine_name' => array(
      'exists' => array(
        $this,
        'exists',
      ),
      'replace_pattern' => '([^a-z0-9_]+)|(^custom$)',
      'error' => 'The machine-readable name must be unique, and can only contain lowercase letters, numbers, and underscores. Additionally, it can not be the reserved word "custom".',
    ),
    '#disabled' => !$content_moderation_notification
      ->isNew(),
  );

  //Transitions
  $state_transitions_options = array();
  $state_transitions = $this->entityTypeManager
    ->getStorage('moderation_state_transition')
    ->loadMultiple();
  foreach ($state_transitions as $key => $transition) {
    $state_transitions_options[$key] = $states[$transition
      ->getFromState()]
      ->label() . ' to ' . $states[$transition
      ->getToState()]
      ->label();
  }
  $form['transitions'] = array(
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Transitions'),
    '#options' => $state_transitions_options,
    '#default_value' => isset($content_moderation_notification->transitions) ? $content_moderation_notification->transitions : array(),
    '#required' => TRUE,
    '#description' => 'Select which transitions triggers this notification.',
  );
  $bundles = \Drupal::service('entity_type.bundle.info')
    ->getBundleInfo('node');
  $bundles_options = array();
  foreach ($bundles as $bundle_key => $bundle) {
    $node_type = NodeType::load($bundle_key);
    if ($node_type
      ->getThirdPartySetting('content_moderation', 'enabled')) {
      $bundles_options[$bundle_key] = $bundle['label'];
    }
  }
  $form['bundles'] = array(
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Bundles'),
    '#options' => $bundles_options,
    '#default_value' => isset($content_moderation_notification->bundles) ? $content_moderation_notification->bundles : array(),
    '#required' => TRUE,
    '#description' => 'Limit notifications to the following bundles.',
  );

  // Role selection
  $roles_options = array();
  foreach (user_roles(TRUE) as $name => $role) {
    $roles_options[$name] = $role
      ->label();
  }
  $form['roles'] = array(
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Roles'),
    '#options' => $roles_options,
    '#default_value' => isset($content_moderation_notification->roles) ? $content_moderation_notification->roles : array(),
    '#description' => 'Send notifications to all users with these roles.',
  );

  // Send email to author?
  $form['author'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Email the author?'),
    '#default_value' => isset($content_moderation_notification->author) ? $content_moderation_notification->author : 0,
    '#description' => 'Send notifications to the current author of the content.',
  );
  $form['emails'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Adhoc email addresses'),
    '#default_value' => isset($content_moderation_notification->emails) ? $content_moderation_notification->emails : '',
    '#description' => 'Send notifications to these email addresses, emails should be entered as a comma separated list.',
  );
  $form['subject'] = [
    '#type' => 'textfield',
    '#title' => t('Email Subject'),
    '#default_value' => isset($content_moderation_notification->subject) ? $content_moderation_notification->subject : '',
    '#empty_value' => '',
  ];

  // Text
  $form['body'] = [
    '#type' => 'text_format',
    '#format' => isset($content_moderation_notification->email) ? $content_moderation_notification->email['format'] : 'basic_html',
    '#title' => t('Email Body'),
    '#default_value' => isset($content_moderation_notification->email) ? $content_moderation_notification->email['value'] : '',
    '#empty_value' => '',
  ];

  // Return the form.
  return $form;
}