You are here

public function UnpublishedNodesRedirectSettingsForm::buildForm in Unpublished Nodes Redirect 2.x

Same name and namespace in other branches
  1. 8 src/Form/UnpublishedNodesRedirectSettingsForm.php \Drupal\unpublished_nodes_redirect\Form\UnpublishedNodesRedirectSettingsForm::buildForm()

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/UnpublishedNodesRedirectSettingsForm.php, line 34

Class

UnpublishedNodesRedirectSettingsForm
Configure example settings for this site.

Namespace

Drupal\unpublished_nodes_redirect\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('unpublished_nodes_redirect.settings');

  // Setup a form input for each node type.
  $content_types = Utils::getNodeTypes();
  foreach ($content_types as $key => $type_name) {

    // Fieldset.
    $form[$type_name] = [
      '#type' => 'fieldset',
      // @todo does the title need to be check plained????
      '#title' => $this
        ->t($type_name),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    ];

    // Redirect path text input.
    $key_name = Utils::getNodeTypeKey($type_name);
    $form[$type_name][$key_name] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('@type internal redirect path', [
        '@type' => $type_name,
      ]),
      '#description' => $this
        ->t('Enter an internal redirect path for the @type content type.', [
        '@type' => $type_name,
      ]),
      '#default_value' => !empty($config
        ->get($key_name)) ? $config
        ->get($key_name) : '',
    ];

    // Redirect response code.
    $key_name = Utils::getResponseCodeKey($type_name);
    $form[$type_name][$key_name] = [
      '#type' => 'select',
      '#title' => t('@type response code', [
        '@type' => $type_name,
      ]),
      '#description' => t('Select a HTTP Response code for the redirect.'),
      '#options' => [
        0 => t('- Please select a response code -'),
        301 => t('301 - Moved Permanently'),
        302 => t('302 - Found'),
        307 => t('307 - Temporary Redirect'),
      ],
      '#default_value' => !empty($config
        ->get($key_name)) ? $config
        ->get($key_name) : '',
    ];
  }
  return parent::buildForm($form, $form_state);
}