You are here

public function SettingsForm::buildForm in Node Option Premium 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 ConfigFormBase::buildForm

File

src/Form/SettingsForm.php, line 85

Class

SettingsForm
Defines a form that configures settings.

Namespace

Drupal\nopremium\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
  $config = $this
    ->config('nopremium.settings');
  $form['messages'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Premium messages'),
    '#description' => $this
      ->t('You may customize the messages displayed to unprivileged users trying to view full premium contents.'),
  ];
  $form['messages']['default_message'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Default message'),
    '#description' => $this
      ->t('This message will apply to all content types with blank messages below.'),
    '#default_value' => $config
      ->get('default_message'),
    '#rows' => 3,
    '#required' => TRUE,
  ];
  foreach ($this->entityTypeManager
    ->getStorage('node_type')
    ->loadMultiple() as $content_type) {
    $form['messages']['message_' . $content_type
      ->id()] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Message for %type content type', [
        '%type' => $content_type
          ->label(),
      ]),
      '#default_value' => $config
        ->get('messages.' . $content_type
        ->id()),
      '#rows' => 3,
    ];
  }
  if ($this->moduleHandler
    ->moduleExists('token')) {
    $form['messages']['token_tree'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => [
        'user',
        'node',
      ],
      '#weight' => 90,
    ];
  }
  else {
    $form['messages']['token_tree'] = [
      '#markup' => '<p>' . $this
        ->t('Enable the <a href="@drupal-token">Token module</a> to view the available token browser.', [
        '@drupal-token' => 'http://drupal.org/project/token',
      ]) . '</p>',
    ];
  }
  $options = [];
  foreach ($this->entityDisplayRepository
    ->getViewModes('node') as $id => $view_mode) {
    $options[$id] = $view_mode['label'];
  }
  $form['view_modes'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Premium display modes'),
    '#description' => $this
      ->t('Select for which view modes access is restricted. When none are selected, all are restricted except the view mode that is selected as "@teaser_view_mode".', [
      '@teaser_view_mode' => $this
        ->t('Teaser display mode'),
    ]),
    '#default_value' => $config
      ->get('view_modes'),
    '#options' => $options,
  ];
  $form['teaser_view_mode'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Teaser display mode'),
    '#description' => $this
      ->t('Teaser display view mode to render for premium contents.'),
    '#default_value' => $config
      ->get('teaser_view_mode'),
    '#options' => $options,
  ];
  return parent::buildForm($form, $form_state);
}