You are here

public function CategoryForm::form in Mass Contact 8

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/CategoryForm.php, line 45

Class

CategoryForm
Class CategoryForm.

Namespace

Drupal\mass_contact\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /** @var \Drupal\mass_contact\Entity\MassContactCategoryInterface $mass_contact_category */
  $mass_contact_category = $this->entity;
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Category'),
    '#maxlength' => 255,
    '#default_value' => $mass_contact_category
      ->label(),
    '#description' => $this
      ->t('The category name to display on the Mass Contact form.'),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $mass_contact_category
      ->id(),
    '#machine_name' => [
      'exists' => '\\Drupal\\mass_contact\\Entity\\MassContactCategory::load',
    ],
    '#disabled' => !$mass_contact_category
      ->isNew(),
  ];
  $form['recipients'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Recipients'),
    '#tree' => TRUE,
    '#open' => TRUE,
  ];

  // Attach plugin forms.
  foreach ($this->groupingMethodManager
    ->getDefinitions() as $definition) {
    if (!($plugin = $mass_contact_category
      ->getGroupingCategories($definition['id']))) {
      $plugin = $this->groupingMethodManager
        ->createInstance($definition['id'], []);
    }
    $form['recipients'][$plugin
      ->getPluginId()] = [];
    $plugin
      ->adminForm($form['recipients'][$plugin
      ->getPluginId()], $form_state);
  }
  $form['selected'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Selected by default'),
    '#default_value' => $mass_contact_category
      ->getSelected(),
    '#description' => $this
      ->t('This category will be selected by default on the <a href="@url">Mass Contact form</a>.', [
      '@url' => Url::fromRoute('entity.mass_contact_message.add_form')
        ->toString(),
    ]),
  ];
  return $form;
}