You are here

public function GroupRoleForm::form in Group 2.0.x

Same name and namespace in other branches
  1. 8 src/Entity/Form/GroupRoleForm.php \Drupal\group\Entity\Form\GroupRoleForm::form()

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/Entity/Form/GroupRoleForm.php, line 39

Class

GroupRoleForm
Form controller for group role forms.

Namespace

Drupal\group\Entity\Form

Code

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

  /** @var \Drupal\group\Entity\GroupRoleInterface $group_role */
  $group_role = $this->entity;
  $group_role_id = '';
  $form['label'] = [
    '#title' => $this
      ->t('Name'),
    '#type' => 'textfield',
    '#default_value' => $group_role
      ->label(),
    '#description' => $this
      ->t('The human-readable name of this group role. This text will be displayed on the group permissions page.'),
    '#required' => TRUE,
    '#size' => 30,
  ];

  // Since group role IDs are prefixed by the group type's ID followed by a
  // period, we need to save some space for that.
  $subtract = strlen($group_role
    ->getGroupTypeId()) + 1;

  // Since machine names with periods in it are technically not allowed, we
  // strip the group type ID prefix when editing a group role.
  if ($group_role
    ->id()) {
    list(, $group_role_id) = explode('-', $group_role
      ->id(), 2);
  }
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $group_role_id,
    '#maxlength' => EntityTypeInterface::ID_MAX_LENGTH - $subtract,
    '#machine_name' => [
      'exists' => [
        $this,
        'exists',
      ],
      'source' => [
        'label',
      ],
    ],
    '#description' => $this
      ->t('A unique machine-readable name for this group role. It must only contain lowercase letters, numbers, and underscores.'),
    '#disabled' => !$group_role
      ->isNew(),
    '#field_prefix' => $group_role
      ->getGroupTypeId() . '-',
  ];
  $form['weight'] = [
    '#type' => 'value',
    '#value' => $group_role
      ->getWeight(),
  ];
  return $form;
}