Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function evaluate()
 {
     $alias = $this->getContextValue('alias');
     $language = $this->getContext('language')->getContextData() ? $this->getContextValue('language')->getId() : NULL;
     $path = $this->aliasManager->getPathByAlias($alias, $language);
     return $path != $alias;
 }
 /**
  * {@inheritdoc}
  */
 public function validateForm(array &$form, FormStateInterface $form_state)
 {
     // Check for empty front page path.
     if (empty($form_state['values']['site_frontpage'])) {
         // Set to default "user".
         form_set_value($form['front_page']['site_frontpage'], 'user', $form_state);
     } else {
         // Get the normal path of the front page.
         form_set_value($form['front_page']['site_frontpage'], $this->aliasManager->getPathByAlias($form_state['values']['site_frontpage']), $form_state);
     }
     // Validate front page path.
     if (!drupal_valid_path($form_state['values']['site_frontpage'])) {
         $form_state->setErrorByName('site_frontpage', $this->t("The path '%path' is either invalid or you do not have access to it.", array('%path' => $form_state['values']['site_frontpage'])));
     }
     // Get the normal paths of both error pages.
     if (!empty($form_state['values']['site_403'])) {
         form_set_value($form['error_page']['site_403'], $this->aliasManager->getPathByAlias($form_state['values']['site_403']), $form_state);
     }
     if (!empty($form_state['values']['site_404'])) {
         form_set_value($form['error_page']['site_404'], $this->aliasManager->getPathByAlias($form_state['values']['site_404']), $form_state);
     }
     // Validate 403 error path.
     if (!empty($form_state['values']['site_403']) && !drupal_valid_path($form_state['values']['site_403'])) {
         $form_state->setErrorByName('site_403', $this->t("The path '%path' is either invalid or you do not have access to it.", array('%path' => $form_state['values']['site_403'])));
     }
     // Validate 404 error path.
     if (!empty($form_state['values']['site_404']) && !drupal_valid_path($form_state['values']['site_404'])) {
         $form_state->setErrorByName('site_404', $this->t("The path '%path' is either invalid or you do not have access to it.", array('%path' => $form_state['values']['site_404'])));
     }
     parent::validateForm($form, $form_state);
 }
Esempio n. 3
0
 /**
  * Builds the response object to be returned. This will be a node.
  * @param $request
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function buildResponseObject($request)
 {
     $alias = $request->query->get('path');
     $path = $this->aliasManager->getPathByAlias($alias);
     // If $path does not contain /node/ it's a result of no alias existing
     // for any nodes in Drupal.
     if (!strstr("/node/", $path)) {
         throw new HttpException(404);
     }
     $parts = explode("/", $path);
     $this->entity = Node::load($parts[2]);
     return true;
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function validateForm(array &$form, FormStateInterface $form_state)
 {
     // Check for empty front page path.
     if ($form_state->isValueEmpty('site_frontpage')) {
         // Set to default "user/login".
         $form_state->setValueForElement($form['front_page']['site_frontpage'], 'user/login');
     } else {
         // Get the normal path of the front page.
         $form_state->setValueForElement($form['front_page']['site_frontpage'], $this->aliasManager->getPathByAlias($form_state->getValue('site_frontpage')));
     }
     // Validate front page path.
     if (!$this->pathValidator->isValid($form_state->getValue('site_frontpage'))) {
         $form_state->setErrorByName('site_frontpage', $this->t("The path '%path' is either invalid or you do not have access to it.", array('%path' => $form_state->getValue('site_frontpage'))));
     }
     // Get the normal paths of both error pages.
     if (!$form_state->isValueEmpty('site_403')) {
         $form_state->setValueForElement($form['error_page']['site_403'], $this->aliasManager->getPathByAlias($form_state->getValue('site_403')));
     }
     if (!$form_state->isValueEmpty('site_404')) {
         $form_state->setValueForElement($form['error_page']['site_404'], $this->aliasManager->getPathByAlias($form_state->getValue('site_404')));
     }
     // Validate 403 error path.
     if (!$form_state->isValueEmpty('site_403') && !$this->pathValidator->isValid($form_state->getValue('site_403'))) {
         $form_state->setErrorByName('site_403', $this->t("The path '%path' is either invalid or you do not have access to it.", array('%path' => $form_state->getValue('site_403'))));
     }
     // Validate 404 error path.
     if (!$form_state->isValueEmpty('site_404') && !$this->pathValidator->isValid($form_state->getValue('site_404'))) {
         $form_state->setErrorByName('site_404', $this->t("The path '%path' is either invalid or you do not have access to it.", array('%path' => $form_state->getValue('site_404'))));
     }
     parent::validateForm($form, $form_state);
 }
Esempio n. 5
0
 /**
  * {@inheritdoc}
  */
 public function validateForm(array &$form, FormStateInterface $form_state)
 {
     $source =& $form_state->getValue('source');
     $source = $this->aliasManager->getPathByAlias($source);
     $alias =& $form_state->getValue('alias');
     // Trim the submitted value of whitespace and slashes. Ensure to not trim
     // the slash on the left side.
     $alias = rtrim(trim(trim($alias), ''), "\\/");
     if ($source[0] !== '/') {
         $form_state->setErrorByName('source', 'The source path has to start with a slash.');
     }
     if ($alias[0] !== '/') {
         $form_state->setErrorByName('alias', 'The alias path has to start with a slash.');
     }
     // Language is only set if language.module is enabled, otherwise save for all
     // languages.
     $langcode = $form_state->getValue('langcode', LanguageInterface::LANGCODE_NOT_SPECIFIED);
     if ($this->aliasStorage->aliasExists($alias, $langcode, $this->path['source'])) {
         $stored_alias = $this->aliasStorage->load(['alias' => $alias, 'langcode' => $langcode]);
         if ($stored_alias['alias'] !== $alias) {
             // The alias already exists with different capitalization as the default
             // implementation of AliasStorageInterface::aliasExists is
             // case-insensitive.
             $form_state->setErrorByName('alias', t('The alias %alias could not be added because it is already in use in this language with different capitalization: %stored_alias.', ['%alias' => $alias, '%stored_alias' => $stored_alias['alias']]));
         } else {
             $form_state->setErrorByName('alias', t('The alias %alias is already in use in this language.', ['%alias' => $alias]));
         }
     }
     if (!$this->pathValidator->isValid(trim($source, '/'))) {
         $form_state->setErrorByName('source', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $source)));
     }
 }
Esempio n. 6
0
 /**
  * Overrides EntityForm::validate().
  */
 public function validate(array $form, array &$form_state)
 {
     $menu_link = $this->buildEntity($form, $form_state);
     $normal_path = $this->pathAliasManager->getPathByAlias($menu_link->link_path);
     if ($menu_link->link_path != $normal_path) {
         drupal_set_message(t('The menu system stores system paths only, but will use the URL alias for display. %link_path has been stored as %normal_path', array('%link_path' => $menu_link->link_path, '%normal_path' => $normal_path)));
         $menu_link->link_path = $normal_path;
         $form_state['values']['link_path'] = $normal_path;
     }
     if (!UrlHelper::isExternal($menu_link->link_path)) {
         $parsed_link = parse_url($menu_link->link_path);
         if (isset($parsed_link['query'])) {
             $menu_link->options['query'] = array();
             parse_str($parsed_link['query'], $menu_link->options['query']);
         } else {
             // Use unset() rather than setting to empty string
             // to avoid redundant serialized data being stored.
             unset($menu_link->options['query']);
         }
         if (isset($parsed_link['fragment'])) {
             $menu_link->options['fragment'] = $parsed_link['fragment'];
         } else {
             unset($menu_link->options['fragment']);
         }
         if (isset($parsed_link['path']) && $menu_link->link_path != $parsed_link['path']) {
             $menu_link->link_path = $parsed_link['path'];
         }
     }
     if (!trim($menu_link->link_path) || !drupal_valid_path($menu_link->link_path, TRUE)) {
         $this->setFormError('link_path', $form_state, $this->t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $menu_link->link_path)));
     }
     parent::validate($form, $form_state);
 }
Esempio n. 7
0
 /**
  * {@inheritdoc}
  */
 public function isReserved($alias, $source, $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED)
 {
     // Check if this alias already exists.
     if ($existing_source = $this->aliasManager->getPathByAlias($alias, $langcode)) {
         if ($existing_source != $alias) {
             // If it is an alias for the provided source, it is allowed to keep using
             // it. If not, then it is reserved.
             return $existing_source != $source;
         }
     }
     // Then check if there is a route with the same path.
     if ($this->isRoute($alias)) {
         return TRUE;
     }
     // Finally check if any other modules have reserved the alias.
     $args = array($alias, $source, $langcode);
     $implementations = $this->moduleHandler->getImplementations('pathauto_is_alias_reserved');
     foreach ($implementations as $module) {
         $result = $this->moduleHandler->invoke($module, 'pathauto_is_alias_reserved', $args);
         if (!empty($result)) {
             // As soon as the first module says that an alias is in fact reserved,
             // then there is no point in checking the rest of the modules.
             return TRUE;
         }
     }
     return FALSE;
 }
 /**
  * {@inheritdoc}
  */
 public function validateForm(array &$form, FormStateInterface $form_state)
 {
     $link = $form_state->getValues();
     // Make sure we trim and normalize the path first.
     $link['loc'] = trim($link['loc']);
     $link['loc'] = $this->aliasManager->getPathByAlias($link['loc'], $link['language']);
     $form_state->setValue('loc', $link['loc']);
     $query = db_select('xmlsitemap');
     $query->fields('xmlsitemap');
     $query->condition('type', 'custom');
     $query->condition('loc', $link['loc']);
     $query->condition('status', 1);
     $query->condition('access', 1);
     $query->condition('language', $link['language']);
     $result = $query->execute()->fetchAssoc();
     if ($result != FALSE) {
         $form_state->setErrorByName('loc', t('There is already an existing link in the sitemap with the path %link.', array('%link' => $link['loc'])));
     }
     try {
         $client = new Client();
         $res = $client->get(Url::fromRoute('<front>', [], array('absolute' => TRUE)) . $link['loc']);
     } catch (ClientException $e) {
         $form_state->setErrorByName('loc', t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $link['loc'])));
     }
     parent::validateForm($form, $form_state);
 }
 /**
  * {@inheritdoc}
  */
 public function validateForm(array &$form, FormStateInterface $form_state)
 {
     $link = $form_state->getValues();
     $link['loc'] = trim($link['loc']);
     $link['loc'] = $this->aliasManager->getPathByAlias($link['loc'], $link['language']);
     $form_state->setValue('loc', $link['loc']);
     try {
         $client = new Client();
         $res = $client->get(Url::fromRoute('<front>', [], array('absolute' => TRUE)) . $link['loc']);
     } catch (ClientException $e) {
         $form_state->setErrorByName('loc', t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $link['loc'])));
     }
     parent::validateForm($form, $form_state);
 }
Esempio n. 10
0
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, array &$form_state)
 {
     // Remove unnecessary values.
     form_state_values_clean($form_state);
     $pid = isset($form_state['values']['pid']) ? $form_state['values']['pid'] : 0;
     $source =& $form_state['values']['source'];
     $source = $this->aliasManager->getPathByAlias($source);
     $alias = $form_state['values']['alias'];
     // Language is only set if language.module is enabled, otherwise save for all
     // languages.
     $langcode = isset($form_state['values']['langcode']) ? $form_state['values']['langcode'] : LanguageInterface::LANGCODE_NOT_SPECIFIED;
     $this->aliasStorage->save($source, $alias, $langcode, $pid);
     drupal_set_message($this->t('The alias has been saved.'));
     $form_state['redirect_route'] = new Url('path.admin_overview');
 }
 public function testNormalBahvior()
 {
     // When providing no language, default is used, but trying to lookup a
     // non existing language should yield the same result
     foreach ([null, 'martian'] as $langcode) {
         $alias = $this->aliasManager->getAliasByPath('duplicate-alias-1', $langcode);
         $this->assertSame('duplicate-alias', $alias);
         $source = $this->aliasManager->getPathByAlias('duplicate-alias', $langcode);
         $this->assertSame('duplicate-alias-1', $source);
         $alias = $this->aliasManager->getAliasByPath('normal-source', $langcode);
         $this->assertSame('normal-alias', $alias);
         $source = $this->aliasManager->getPathByAlias('normal-alias', $langcode);
         $this->assertSame('normal-source', $source);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     // Remove unnecessary values.
     $form_state->cleanValues();
     $pid = $form_state->getValue('pid', 0);
     $source =& $form_state->getValue('source');
     $source = $this->aliasManager->getPathByAlias($source);
     $alias = $form_state->getValue('alias');
     // Language is only set if language.module is enabled, otherwise save for all
     // languages.
     $langcode = $form_state->getValue('langcode', LanguageInterface::LANGCODE_NOT_SPECIFIED);
     $this->aliasStorage->save($source, $alias, $langcode, $pid);
     drupal_set_message($this->t('The alias has been saved.'));
     $form_state->setRedirect('path.admin_overview');
 }
Esempio n. 13
0
 /**
  * {@inheritdoc}
  */
 public function validateForm(array &$form, FormStateInterface $form_state)
 {
     $source =& $form_state->getValue('source');
     $source = $this->aliasManager->getPathByAlias($source);
     $alias =& $form_state->getValue('alias');
     // Trim the submitted value of whitespace and slashes.
     $alias = trim(trim($alias), " \\/");
     // Language is only set if language.module is enabled, otherwise save for all
     // languages.
     $langcode = $form_state->getValue('langcode', LanguageInterface::LANGCODE_NOT_SPECIFIED);
     if ($this->aliasStorage->aliasExists($alias, $langcode, $this->path['source'])) {
         $form_state->setErrorByName('alias', t('The alias %alias is already in use in this language.', array('%alias' => $alias)));
     }
     if (!$this->pathValidator->isValid($source)) {
         $form_state->setErrorByName('source', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $source)));
     }
 }
Esempio n. 14
0
 /**
  * {@inheritdoc}
  */
 public function processInbound($path, Request $request)
 {
     $path = $this->aliasManager->getPathByAlias($path);
     return $path;
 }
 /**
  * {@inheritdoc}
  */
 public function on404(GetResponseForExceptionEvent $event)
 {
     $path = $this->aliasManager->getPathByAlias($this->configFactory->get('system.site')->get('page.404'));
     $this->makeSubrequest($event, $path, Response::HTTP_NOT_FOUND);
 }
Esempio n. 16
0
 /**
  * Check if a path alias exists.
  *
  * @param string $alias
  *   The alias to see if exists.
  * @param \Drupal\Core\Language\LanguageInterface|null $language
  *   The language to use.
  *
  * @return bool
  *   TRUE if the system path does not match the given alias (ie: the alias
  *   exists).
  */
 protected function doEvaluate($alias, LanguageInterface $language = NULL)
 {
     $lang_code = is_null($language) ? NULL : $language->getId();
     $path = $this->aliasManager->getPathByAlias($alias, $lang_code);
     return $path != $alias;
 }