You are here

public function AgreementForm::form in Agreement 8.2

Same name and namespace in other branches
  1. 3.0.x src/Entity/AgreementForm.php \Drupal\agreement\Entity\AgreementForm::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/AgreementForm.php, line 48

Class

AgreementForm
Add or edit agreements.

Namespace

Drupal\agreement\Entity

Code

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

  /* @var \Drupal\agreement\Entity\Agreement $entity */
  $entity = $this->entity;
  $settings = $entity
    ->getSettings();

  // @todo https://drupal.org/node/2403359
  if (!$entity
    ->isNew()) {
    $form['#title'] = $this
      ->t('Manage Agreement: @label', [
      '@label' => $entity
        ->label(),
    ]);
  }
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Agreement type'),
    '#description' => $this
      ->t('Provide a human-readable label for this agreement type.'),
    '#default_value' => $entity
      ->label(),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => !$entity
      ->isNew() ? $entity
      ->id() : '',
    '#required' => TRUE,
    '#disabled' => !$entity
      ->isNew(),
    '#maxlength' => 32,
    '#machine_name' => [
      'exists' => [
        $this,
        'exists',
      ],
      'source' => [
        'type',
      ],
    ],
  ];
  $form['path'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Path'),
    '#description' => $this
      ->t('At what URL should the agreement page be located? Relative to site root. A leading slash is required.'),
    '#default_value' => $entity
      ->get('path') ? $entity
      ->get('path') : '',
    '#required' => TRUE,
    '#element_validate' => [
      [
        $this,
        'validatePath',
      ],
    ],
  ];
  $form['agreement'] = [
    '#type' => 'text_format',
    '#title' => $this
      ->t('Agreement text'),
    '#description' => $this
      ->t('Provide the agreement text.'),
    '#default_value' => $entity
      ->get('agreement') ? $entity
      ->get('agreement') : '',
    '#format' => $settings['format'] ? $settings['format'] : filter_default_format(),
    '#rows' => 12,
  ];
  $role_options = [];
  $roles = user_roles(FALSE);
  foreach ($roles as $role_name => $role) {
    if (!$role
      ->isAdmin()) {
      $role_options[$role
        ->id()] = $role
        ->label();
    }
  }
  $form['config'] = [
    '#type' => 'vertical_tabs',
    '#title' => $this
      ->t('Settings'),
    '#default_tab' => 'edit-visibility',
  ];
  $form['visibility'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Visibility'),
    '#group' => 'config',
    '#parents' => [
      'settings',
      'visibility',
    ],
  ];
  $form['settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Settings'),
    '#group' => 'config',
    '#parents' => [
      'settings',
    ],
  ];
  $form['visibility']['settings'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Show agreement on specific pages'),
    '#options' => [
      0 => $this
        ->t('Show on every page except the listed pages'),
      1 => $this
        ->t('Show on only the listed pages'),
    ],
    '#required' => TRUE,
    '#default_value' => $entity
      ->getVisibilitySetting(),
    '#parents' => [
      'settings',
      'visibility',
      'settings',
    ],
  ];
  $form['visibility']['pages'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Pages'),
    '#description' => $this
      ->t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths\n                                  are %blog for the blog page and %blog-wildcard for every personal blog. %front is the\n                                  front page. The user password and reset pages will always be excluded.", [
      '%blog' => 'blog',
      '%blog-wildcard' => 'blog/*',
      '%front' => '<front>',
    ]),
    '#default_value' => $entity
      ->getVisibilityPages(),
    '#parents' => [
      'settings',
      'visibility',
      'pages',
    ],
  ];
  $form['settings']['roles'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Roles'),
    '#description' => $this
      ->t('Select all of the roles that are required to accept this agreement.'),
    '#default_value' => $settings['roles'],
    '#required' => TRUE,
    '#multiple' => TRUE,
    '#options' => $role_options,
    '#parents' => [
      'settings',
      'roles',
    ],
  ];
  $form['settings']['frequency'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Frequency'),
    '#description' => $this
      ->t('How ofter should users be required to accept the agreement?'),
    '#options' => [
      -1 => $this
        ->t('Only once'),
      0 => $this
        ->t('On every log in'),
      365 => $this
        ->t('Once a year'),
    ],
    '#required' => TRUE,
    '#default_value' => $settings['frequency'],
    '#parents' => [
      'settings',
      'frequency',
    ],
  ];
  $form['settings']['title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Agreement Page Title'),
    '#description' => $this
      ->t('What should the title of the agreement page be?'),
    '#required' => TRUE,
    '#default_value' => $settings['title'],
    '#parents' => [
      'settings',
      'title',
    ],
  ];
  $form['settings']['checkbox'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Agreement Checkbox Text'),
    '#description' => $this
      ->t('This text will be displayed next to the "I agree" checkbox.'),
    '#required' => TRUE,
    '#default_value' => $settings['checkbox'],
    '#parents' => [
      'settings',
      'checkbox',
    ],
  ];
  $form['settings']['submit'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Agreement Submit Text'),
    '#description' => $this
      ->t('This text will be displayed on the "Submit" button.'),
    '#required' => TRUE,
    '#default_value' => $settings['submit'],
    '#parents' => [
      'settings',
      'submit',
    ],
  ];
  $form['settings']['success'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Agreement Success Message'),
    '#description' => $this
      ->t('What message should be displayed to the users once they accept the agreement?'),
    '#required' => TRUE,
    '#default_value' => $settings['success'],
    '#parents' => [
      'settings',
      'success',
    ],
  ];
  $form['settings']['failure'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Agreement Failure Message'),
    '#description' => $this
      ->t('What message should be displayed to the users if they do not accept the agreement?'),
    '#required' => TRUE,
    '#default_value' => $settings['failure'],
    '#parents' => [
      'settings',
      'failure',
    ],
  ];
  $form['settings']['revoked'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Agreement Revoke Message'),
    '#description' => $this
      ->t('What message should be displayed to the users if they revoke their agreement?'),
    '#default_value' => $settings['revoked'],
    '#parents' => [
      'settings',
      'revoked',
    ],
  ];
  $form['settings']['destination'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Agreement Success Destination'),
    '#description' => $this
      ->t('What page should be displayed after the user accepts the agreement? Leave blank
                                  to go to the original destination that triggered the agreement or the front page
                                  if no original destination is present. %front is the front page. Users who log
                                  in via the one-time login link will always be redirected to their user profile
                                  to change their password.', [
      '%front' => '<front>',
    ]),
    '#default_value' => $settings['destination'],
    '#parents' => [
      'settings',
      'destination',
    ],
  ];
  $form['settings']['recipient'] = [
    '#type' => 'email',
    '#title' => $this
      ->t('Recipient Email'),
    '#description' => $this
      ->t('Optionally sends an email to the provided email address each time any user agrees to this agreement. This transmits personal data.'),
    '#default_value' => $settings['recipient'],
    '#parents' => [
      'settings',
      'recipient',
    ],
  ];
  return parent::form($form, $form_state);
}