You are here

class LoginDestinationRuleForm in Login Destination 8.2

Same name and namespace in other branches
  1. 8 src/Form/LoginDestinationRuleForm.php \Drupal\login_destination\Form\LoginDestinationRuleForm

Base for controller for login destination add/edit forms.

Hierarchy

Expanded class hierarchy of LoginDestinationRuleForm

File

src/Form/LoginDestinationRuleForm.php, line 17

Namespace

Drupal\login_destination\Form
View source
class LoginDestinationRuleForm extends EntityForm {
  use StringTranslationTrait;

  /**
   * The login destination entity storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $loginDestinationStorage;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * Constructs a base class for login destination add and edit forms.
   *
   * @param \Drupal\Core\Entity\EntityStorageInterface $login_destination_storage
   *   The login destination entity storage.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   */
  public function __construct(EntityStorageInterface $login_destination_storage, LanguageManagerInterface $language_manager) {
    $this->loginDestinationStorage = $login_destination_storage;
    $this->languageManager = $language_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager')
      ->getStorage('login_destination'), $container
      ->get('language_manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {

    /** @var \Drupal\login_destination\Entity\LoginDestination $login_destination */
    $login_destination = $this->entity;
    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label'),
      '#default_value' => $login_destination
        ->getLabel(),
      '#description' => $this
        ->t('A short description of this login destination rule.'),
      '#required' => TRUE,
    ];
    $form['name'] = [
      '#type' => 'machine_name',
      '#machine_name' => [
        'exists' => [
          $this->loginDestinationStorage,
          'load',
        ],
      ],
      '#disabled' => !$login_destination
        ->isNew(),
      '#default_value' => $login_destination
        ->id(),
      '#required' => TRUE,
      '#description' => $this
        ->t('A unique machine-readable name for this login destination rule.'),
    ];
    $form['triggers'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Redirect upon triggers'),
      '#options' => [
        LoginDestination::TRIGGER_REGISTRATION => $this
          ->t('Registration'),
        LoginDestination::TRIGGER_LOGIN => $this
          ->t('Login'),
        LoginDestination::TRIGGER_ONE_TIME_LOGIN => $this
          ->t('One-time login link'),
        LoginDestination::TRIGGER_LOGOUT => $this
          ->t('Logout'),
      ],
      '#required' => TRUE,
      '#default_value' => !empty($login_destination->triggers) ? $login_destination
        ->getTriggers() : [],
      '#description' => $this
        ->t('Redirect only upon selected trigger(s).'),
    ];
    $form['destination_path'] = [
      '#type' => 'entity_autocomplete',
      '#target_type' => 'node',
      '#placeholder' => '',
      '#attributes' => [
        'data-autocomplete-first-character-blacklist' => '/#?[',
      ],
      '#title' => $this
        ->t('Redirect destination'),
      '#default_value' => $this
        ->getUriAsDisplayableString($login_destination
        ->getDestination()),
      '#element_validate' => [
        [
          $this,
          'validateUriElement',
        ],
      ],
      '#maxlength' => 2048,
      '#required' => TRUE,
      '#process_default_value' => FALSE,
      '#description' => $this
        ->t('Start typing the title of a piece of content to select it. You can also enter an internal path such as %add-node or an external URL such as %url. Enter %front to link to the front page. Enter %current to link to a current page.', [
        '%front' => '<front>',
        '%current' => '<current>',
        '%add-node' => '/node/add',
        '%url' => 'http://example.com',
      ]),
    ];

    // Add the token tree UI.
    $form['token_tree'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => [
        'user',
      ],
      '#show_restricted' => TRUE,
      '#global_types' => TRUE,
    ];
    $form['pages_type'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Redirect from specific pages'),
      '#default_value' => $login_destination
        ->getPagesType(),
      '#options' => [
        $login_destination::REDIRECT_NOT_LISTED => $this
          ->t('All pages except those listed'),
        $login_destination::REDIRECT_LISTED => $this
          ->t('Only the listed pages'),
      ],
    ];
    $form['pages'] = [
      '#type' => 'textarea',
      '#default_value' => $login_destination
        ->getPages(),
      '#description' => $this
        ->t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page. %login is the login form. %register is the registration form. %reset is the one-time login (e-mail validation).", [
        '%blog' => 'blog',
        '%blog-wildcard' => '/blog/*',
        '%front' => '<front>',
        '%login' => '/user',
        '%register' => '/user/register',
        '%reset' => '/user/*/edit',
      ]),
    ];
    $languages[''] = $this
      ->t('All languages');
    foreach ($this->languageManager
      ->getLanguages() as $key => $value) {
      $languages[$key] = $value
        ->getName();
    }
    $form['language'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Redirect for language'),
      '#options' => $languages,
      '#default_value' => $login_destination
        ->getLanguage(),
      '#description' => $this
        ->t('Redirect only for the selected language.'),
    ];
    $form['roles'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Redirect users with roles'),
      '#options' => $login_destination
        ->getAllSystemRoles(),
      '#default_value' => $login_destination
        ->getRoles(),
      '#description' => $this
        ->t('Redirect only the selected role(s). If you select no roles, all users will be redirected.'),
    ];
    $form['uuid'] = [
      '#type' => 'value',
      '#value' => $login_destination
        ->get('uuid'),
    ];
    return parent::form($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);

    // Get selected roles.
    $roles = array_filter($form_state
      ->getValue('roles'));

    // Set filtered values.
    $form_state
      ->setValue('roles', $roles);
    $form_state
      ->setValue('triggers', array_filter($form_state
      ->getValue('triggers')));

    // Get entered by user destination path.
    // $destination = $form_state->getValue('destination_path');.
    // @todo verify that selected role has access to entered path.
    // @todo verify entered paths to specific pages.
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {

    /** @var  \Drupal\login_destination\Entity\LoginDestination $login_destination */
    $login_destination = $this->entity;
    if ($login_destination
      ->save()) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('Saved the %label login destination.', [
        '%label' => $login_destination
          ->getLabel(),
      ]));
    }
    else {
      $this
        ->messenger()
        ->addMessage($this
        ->t('The %label login destination was not saved.', [
        '%label' => $login_destination
          ->getLabel(),
      ]));
    }
    $form_state
      ->setRedirect('login_destination.list');
  }

  /**
   * Form element validation handler for the 'uri' element.
   *
   * Disallows saving inaccessible or untrusted URLs.
   *
   * @see LinkWidget::validateUriElement()
   */
  public function validateUriElement($element, FormStateInterface $form_state, $form) {
    $uri = $this
      ->getUserEnteredStringAsUri($element['#value']);
    $form_state
      ->setValueForElement($element, $uri);

    // If getUserEnteredStringAsUri() mapped the entered value to a 'internal:'
    // URI , ensure the raw value begins with '/', '?' or '#'.
    // @todo '<front>' is valid input for BC reasons, may be removed by
    //   https://www.drupal.org/node/2421941
    if (parse_url($uri, PHP_URL_SCHEME) === 'internal' && !in_array($element['#value'][0], [
      '/',
      '?',
      '#',
      '[',
    ], TRUE) && substr($element['#value'], 0, 7) !== '<front>' && substr($element['#value'], 0, 9) !== '<current>') {
      $form_state
        ->setError($element, $this
        ->t('Manually entered paths should start with /, [, ? or #.'));
      return;
    }
  }

  /**
   * Gets the URI without the 'internal:' or 'entity:' scheme.
   *
   * The following two forms of URIs are transformed:
   * - 'entity:' URIs: to entity autocomplete ("label (entity id)") strings;
   * - 'internal:' URIs: the scheme is stripped.
   *
   * This method is the inverse of ::getUserEnteredStringAsUri().
   *
   * @param string $uri
   *   The URI to get the displayable string for.
   *
   * @return string
   *   Returns a $displayable_string for the URI.
   * @see LinkWidget::getUriAsDisplayableString()
   */
  protected function getUriAsDisplayableString($uri) {
    $scheme = parse_url($uri, PHP_URL_SCHEME);

    // By default, the displayable string is the URI.
    $displayable_string = $uri;

    // A different displayable string may be chosen in case of the 'internal:'
    // or 'entity:' built-in schemes.
    if ($scheme === 'internal') {
      $uri_reference = explode(':', $uri, 2)[1];

      // @todo '<front>' is valid input for BC reasons, may be removed by
      //   https://www.drupal.org/node/2421941
      $path = parse_url($uri, PHP_URL_PATH);
      if ($path === '/') {
        $uri_reference = '<front>' . substr($uri_reference, 1);
      }
      $displayable_string = $uri_reference;
    }
    elseif ($scheme === 'entity') {
      list($entity_type, $entity_id) = explode('/', substr($uri, 7), 2);

      // Show the 'entity:' URI as the entity autocomplete would.
      if ($this->entityTypeManager
        ->getDefinition($entity_type, FALSE) && ($entity = $this->entityTypeManager
        ->getStorage($entity_type)
        ->load($entity_id))) {
        $displayable_string = EntityAutocomplete::getEntityLabels([
          $entity,
        ]);
      }
    }
    return $displayable_string;
  }

  /**
   * Gets the user-entered string as a URI.
   *
   * The following two forms of input are mapped to URIs:
   * - entity autocomplete ("label (entity id)") strings: to 'entity:' URIs;
   * - strings without a detectable scheme: to 'internal:' URIs.
   *
   * This method is the inverse of ::getUriAsDisplayableString().
   *
   * @param string $string
   *   The user-entered string.
   *
   * @return string
   *   The URI, if a non-empty $uri was passed.
   *
   * @see LinkWidget::getUserEnteredStringAsUri()
   */
  protected function getUserEnteredStringAsUri($string) {

    // By default, assume the entered string is an URI.
    $uri = $string;

    // Detect entity autocomplete string, map to 'entity:' URI.
    $entity_id = EntityAutocomplete::extractEntityIdFromAutocompleteInput($string);
    if ($entity_id !== NULL) {

      // @todo Support entity types other than 'node'. Will be fixed in
      //   https://www.drupal.org/node/2423093.
      $uri = 'entity:node/' . $entity_id;
    }
    elseif (!empty($string) && parse_url($string, PHP_URL_SCHEME) === NULL) {

      // @todo '<front>' is valid input for BC reasons, may be removed by
      //   https://www.drupal.org/node/2421941
      // - '<front>' -> '/'
      // - '<front>#foo' -> '/#foo'
      if (strpos($string, '<front>') === 0) {
        $string = '/' . substr($string, strlen('<front>'));
      }
      if (strpos($string, '[') === 0) {
        $string = '/' . $string;
      }
      $uri = 'internal:' . $string;
    }
    return $uri;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityForm::$entity protected property The entity being used by this form. 7
EntityForm::$entityTypeManager protected property The entity type manager. 3
EntityForm::$moduleHandler protected property The module handler service.
EntityForm::$operation protected property The name of the current operation.
EntityForm::$privateEntityManager private property The entity manager.
EntityForm::actions protected function Returns an array of supported actions for the current entity form. 29
EntityForm::actionsElement protected function Returns the action form element for the current entity form.
EntityForm::afterBuild public function Form element #after_build callback: Updates the entity with submitted data.
EntityForm::buildEntity public function Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface::buildEntity 2
EntityForm::buildForm public function Form constructor. Overrides FormInterface::buildForm 10
EntityForm::copyFormValuesToEntity protected function Copies top-level form values to entity properties 7
EntityForm::getBaseFormId public function Returns a string identifying the base form. Overrides BaseFormIdInterface::getBaseFormId 5
EntityForm::getEntity public function Gets the form entity. Overrides EntityFormInterface::getEntity
EntityForm::getEntityFromRouteMatch public function Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface::getEntityFromRouteMatch 1
EntityForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId 10
EntityForm::getOperation public function Gets the operation identifying the form. Overrides EntityFormInterface::getOperation
EntityForm::init protected function Initialize the form state and the entity before the first form build. 3
EntityForm::prepareEntity protected function Prepares the entity object before the form is built first. 3
EntityForm::prepareInvokeAll protected function Invokes the specified prepare hook variant.
EntityForm::processForm public function Process callback: assigns weights and hides extra fields.
EntityForm::setEntity public function Sets the form entity. Overrides EntityFormInterface::setEntity
EntityForm::setEntityManager public function Sets the entity manager for this form. Overrides EntityFormInterface::setEntityManager
EntityForm::setEntityTypeManager public function Sets the entity type manager for this form. Overrides EntityFormInterface::setEntityTypeManager
EntityForm::setModuleHandler public function Sets the module handler for this form. Overrides EntityFormInterface::setModuleHandler
EntityForm::setOperation public function Sets the operation for this form. Overrides EntityFormInterface::setOperation
EntityForm::submitForm public function This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state… Overrides FormInterface::submitForm 17
EntityForm::__get public function
EntityForm::__set public function
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
LoginDestinationRuleForm::$languageManager protected property The language manager.
LoginDestinationRuleForm::$loginDestinationStorage protected property The login destination entity storage.
LoginDestinationRuleForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
LoginDestinationRuleForm::form public function Gets the actual form array to be built. Overrides EntityForm::form
LoginDestinationRuleForm::getUriAsDisplayableString protected function Gets the URI without the 'internal:' or 'entity:' scheme.
LoginDestinationRuleForm::getUserEnteredStringAsUri protected function Gets the user-entered string as a URI.
LoginDestinationRuleForm::save public function Form submission handler for the 'save' action. Overrides EntityForm::save
LoginDestinationRuleForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
LoginDestinationRuleForm::validateUriElement public function Form element validation handler for the 'uri' element.
LoginDestinationRuleForm::__construct public function Constructs a base class for login destination add and edit forms.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.