You are here

public function InviteTypeForm::form in Invite 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/InviteTypeForm.php, line 76

Class

InviteTypeForm
Form controller for Invite type edit forms.

Namespace

Drupal\invite\Form

Code

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

  /* @var $entity \Drupal\invite\Entity\InviteType */
  $form = parent::form($form, $form_state);
  $entity = $this->entity;
  $is_new = $entity
    ->isNew();
  if ($is_new) {
    $entity
      ->set('label', '')
      ->set('type', '')
      ->set('description', '')
      ->set('data', '');
  }
  $data = unserialize($entity
    ->getData());
  $form['label'] = [
    '#title' => $this
      ->t('Invite Type Label'),
    '#type' => 'textfield',
    '#default_value' => $entity
      ->label(),
    '#description' => $this
      ->t('The human-readable name of this invite type. This name must be unique.'),
    '#required' => TRUE,
    '#size' => 30,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $entity
      ->getType(),
    '#maxlength' => 255,
    '#disabled' => !$is_new,
    '#machine_name' => [
      'exists' => [
        'Drupal\\invite\\Entity\\InviteType',
        'load',
      ],
      'source' => [
        'label',
      ],
    ],
    '#description' => $this
      ->t('A unique machine-readable name for this invite type. It must only contain lowercase letters, numbers, and underscores.'),
  ];
  $form['description'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Description'),
    '#description' => $this
      ->t('Description about the invite type.'),
    '#rows' => 5,
    '#default_value' => $entity
      ->getDescription(),
  ];
  $options[] = '- ' . $this
    ->t('None') . ' -';
  foreach (user_roles() as $user_role) {
    if (empty($user_role
      ->get('_core'))) {
      $options[$user_role
        ->id()] = $user_role
        ->label();
    }
  }
  $form['target_role'] = [
    '#type' => 'select',
    '#required' => FALSE,
    '#title' => $this
      ->t('Role'),
    '#description' => $this
      ->t('Please select a role to apply to the invitee (Optional).'),
    '#options' => $options,
    '#default_value' => $data['target_role'],
  ];

  // List the available sending methods.
  $plugin_definitions = $this->pluginManager
    ->getDefinitions();
  if (!empty($plugin_definitions)) {
    $options = [];
    foreach ($plugin_definitions as $plugin_definition) {
      $options[$plugin_definition['provider']] = $plugin_definition['id'];
    }
    $default_send_method = [];
    if (!$is_new) {
      $default_send_method = $this
        ->getDefaultSendMethods($entity);
    }
    $form['send_method'] = [
      '#type' => 'checkboxes',
      '#required' => TRUE,
      '#title' => $this
        ->t('Sending Method'),
      '#default_value' => $default_send_method,
      '#options' => $options,
    ];
  }
  else {
    $form['send_method'] = [
      '#type' => 'item',
      '#markup' => $this
        ->t('Please enable a sending method module such as Invite by email.'),
    ];
    $form['actions']['submit']['#disabled'] = TRUE;
  }
  return $form;
}