You are here

public function SitewideAlertForm::buildForm in Sitewide Alert 8

Form constructor.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EntityForm::buildForm

File

src/Form/SitewideAlertForm.php, line 61

Class

SitewideAlertForm
Form controller for Sitewide Alert edit forms.

Namespace

Drupal\sitewide_alert\Form

Code

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

  /* @var \Drupal\sitewide_alert\Entity\SitewideAlertInterface $entity */
  $entity = $this->entity;
  $form = parent::buildForm($form, $form_state);

  // Authoring information for administrators.
  if (isset($form['user_id'])) {
    $form['author'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Authoring information'),
      '#group' => 'advanced',
      '#attributes' => [
        'class' => [
          'sitewide_alert-form-author',
        ],
      ],
      '#weight' => -3,
      '#optional' => TRUE,
    ];
    $form['user_id']['#group'] = 'author';
  }

  // Make the scheduled alert dates conditional on the checkbox.
  $form['scheduled_date']['#states'] = [
    'visible' => [
      ':input[name="scheduled_alert[value]"]' => [
        'checked' => TRUE,
      ],
    ],
  ];

  // Allow the editor to disable previous dismissals.
  if (!$entity
    ->isNew()) {
    $form['dismissible_ignore_previous'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Ignore Previous Dismissals'),
      '#description' => $this
        ->t('Select this when making a major change and you want to make sure all visitors see this alert even if they have dismissed it prior.'),
      '#default_value' => FALSE,
      '#return_value' => TRUE,
      '#weight' => -9,
      '#states' => [
        'visible' => [
          ':input[name="dismissible[value]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
  }

  // Organize the limit by pages options.
  $form['limit_by_pages_fieldset'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Page Visibility'),
    '#description' => $this
      ->t('Limit the alert to only show on some of pages.'),
  ];
  $form['limit_by_pages_fieldset']['limit_alert_by_pages'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Limit by Page'),
    '#default_value' => !empty($entity
      ->getPagesToShowOn()),
    '#return_value' => TRUE,
    '#weight' => -10,
  ];
  $form['limit_by_pages_fieldset']['limit_to_pages'] = $form['limit_to_pages'];
  unset($form['limit_to_pages']);
  $form['limit_by_pages_fieldset']['limit_to_pages']['#states'] = [
    'visible' => [
      ':input[name="limit_alert_by_pages"]' => [
        'checked' => TRUE,
      ],
    ],
  ];
  $form['limit_by_pages_fieldset']['limit_to_pages_negate'] = $form['limit_to_pages_negate'];
  unset($form['limit_to_pages_negate']);
  $form['limit_by_pages_fieldset']['limit_to_pages_negate']['#states'] = [
    'visible' => [
      ':input[name="limit_alert_by_pages"]' => [
        'checked' => TRUE,
      ],
    ],
  ];
  return $form;
}