You are here

public function PageRedirect::settingsForm in Rabbit Hole 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/RabbitHoleBehaviorPlugin/PageRedirect.php \Drupal\rabbit_hole\Plugin\RabbitHoleBehaviorPlugin\PageRedirect::settingsForm()

Return a settings form for the rabbit hole action.

Parameters

array &$form: The form array to modify.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

string $form_id: The form ID.

\Drupal\Core\Entity\EntityInterface|null $entity: The entity used by the form.

bool $entity_is_bundle: Whether the entity is a bundle.

\Drupal\Core\Config\ImmutableConfig|null $bundle_settings: The behavior settings for the bundle of the entity (or the entity itself, if it is a bundle).

Overrides RabbitHoleBehaviorPluginBase::settingsForm

File

src/Plugin/RabbitHoleBehaviorPlugin/PageRedirect.php, line 260

Class

PageRedirect
Redirects to another page.

Namespace

Drupal\rabbit_hole\Plugin\RabbitHoleBehaviorPlugin

Code

public function settingsForm(array &$form, FormStateInterface $form_state, $form_id, EntityInterface $entity = NULL, $entity_is_bundle = FALSE, ImmutableConfig $bundle_settings = NULL) {
  $redirect = NULL;
  $redirect_code = NULL;
  $redirect_fallback_action = NULL;
  if ($entity_is_bundle) {
    $redirect = $bundle_settings
      ->get('redirect');
    $redirect_code = $bundle_settings
      ->get('redirect_code');
    $redirect_fallback_action = $bundle_settings
      ->get('redirect_fallback_action');
  }
  elseif (isset($entity)) {
    $redirect = isset($entity->rh_redirect->value) ? $entity->rh_redirect->value : self::RABBIT_HOLE_PAGE_REDIRECT_DEFAULT;
    $redirect_code = isset($entity->rh_redirect_response->value) ? $entity->rh_redirect_response->value : self::RABBIT_HOLE_PAGE_REDIRECT_RESPONSE_DEFAULT;
    $redirect_fallback_action = isset($entity->rh_redirect_fallback_action->value) ? $entity->rh_redirect_fallback_action->value : 'bundle_default';
  }
  else {
    $redirect = NULL;
    $redirect_code = NULL;
  }
  $form['rabbit_hole']['redirect'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Redirect settings'),
    '#attributes' => [
      'class' => [
        'rabbit-hole-redirect-options',
      ],
    ],
    '#states' => [
      'visible' => [
        ':input[name="rh_action"]' => [
          'value' => $this
            ->getPluginId(),
        ],
      ],
    ],
  ];

  // Get the default value for the redirect path.
  // Build the descriptive text.
  $description = [];
  $description[] = $this
    ->t('Enter the %front tag, relative path or the full URL that the user should get redirected to. Query strings and fragments are supported, such as %example.', [
    '%front' => '<front>',
    '%example' => 'http://www.example.com/?query=value#fragment',
  ]);
  $description[] = $this
    ->t('You may enter tokens in this field, such as %example1 or %example2.', [
    '%example1' => '[node:field_link]',
    '%example2' => '/my/view?page=[node:field_page_number]',
  ]);
  $form['rabbit_hole']['redirect']['rh_redirect'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Redirect path'),
    '#default_value' => $redirect,
    '#description' => '<p>' . implode('</p><p>', $description) . '</p>',
    '#attributes' => [
      'class' => [
        'rabbit-hole-redirect-setting',
      ],
    ],
    '#rows' => substr_count($redirect, "\r\n") + 2,
    '#element_validate' => [],
    '#after_build' => [],
    '#states' => [
      'required' => [
        ':input[name="rh_action"]' => [
          'value' => $this
            ->getPluginId(),
        ],
      ],
    ],
    '#maxlength' => 2000,
  ];
  $entity_type_id = NULL;
  if (isset($entity)) {
    $entity_type_id = $entity_is_bundle ? $entity
      ->getEntityType()
      ->getBundleOf() : $entity
      ->getEntityTypeId();
  }
  else {
    $entity_type_id = $this->rhEntityPluginManager
      ->loadSupportedGlobalForms()[$form_id];
  }
  $entity_type_for_tokens = NULL;
  if ($this->moduleHandler
    ->moduleExists('token')) {
    $token_map = $this->rhEntityPluginManager
      ->loadEntityTokenMap();
    $entity_type_for_tokens = $token_map[$entity_type_id];
    $form['rabbit_hole']['redirect']['rh_redirect']['#element_validate'][] = 'token_element_validate';
    $form['rabbit_hole']['redirect']['rh_redirect']['#after_build'][] = 'token_element_validate';
    $form['rabbit_hole']['redirect']['rh_redirect']['#token_types'] = [
      $entity_type_for_tokens,
    ];
  }

  // Add the redirect response setting.
  $form['rabbit_hole']['redirect']['rh_redirect_response'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Response code'),
    '#options' => [
      301 => $this
        ->t('301 (Moved Permanently)'),
      302 => $this
        ->t('302 (Found)'),
      303 => $this
        ->t('303 (See other)'),
      304 => $this
        ->t('304 (Not modified)'),
      305 => $this
        ->t('305 (Use proxy)'),
      307 => $this
        ->t('307 (Temporary redirect)'),
    ],
    '#default_value' => $redirect_code,
    '#description' => $this
      ->t('The response code that should be sent to the users browser. Follow @link for more information on response codes.', [
      '@link' => Link::fromTextAndUrl($this
        ->t('this link'), Url::fromUri('http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_goto/7'))
        ->toString(),
    ]),
    '#attributes' => [
      'class' => [
        'rabbit-hole-redirect-response-setting',
      ],
    ],
  ];

  // Add fallback action setting with all available options except page
  // redirect.
  $fallback_options = $form['rh_action']['#options'];
  unset($fallback_options['page_redirect']);
  if (isset($fallback_options['bundle_default'])) {
    $args = $fallback_options['bundle_default']
      ->getArguments();
    $bundle_settings = $this
      ->getBundleSettings($entity);
    $bundle_fallback = $bundle_settings
      ->get('redirect_fallback_action');
    $fallback_options['bundle_default'] = $this
      ->t('Global @bundle fallback (@setting)', [
      '@bundle' => $args['@bundle'],
      '@setting' => $bundle_fallback,
    ]);
  }
  $form['rabbit_hole']['redirect']['rh_redirect_fallback_action'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Fallback behavior'),
    '#options' => $fallback_options,
    '#default_value' => $redirect_fallback_action,
    '#description' => $this
      ->t('What should happen when the redirect is invalid/empty?'),
    '#attributes' => [
      'class' => [
        'rabbit-hole-redirect-fallback-action-setting',
      ],
    ],
  ];

  // Display a list of tokens if the Token module is enabled.
  if ($this->moduleHandler
    ->moduleExists('token')) {
    $form['rabbit_hole']['redirect']['token_help'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => [
        $entity_type_for_tokens,
      ],
    ];
  }
}