/** * {@inheritdoc} */ public function validate(array $form, FormStateInterface $form_state) { if (!$this->pathValidator->isValid($form_state->getValue('path'))) { $form_state->setErrorByName('path', $this->t('The shortcut must correspond to a valid path on the site.')); } parent::validate($form, $form_state); }
/** * {@inheritdoc} * * Set the destination route information based on the source link_path. */ public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { list($link_path, $options) = $value; $extracted = $this->pathValidator->getUrlIfValidWithoutAccessCheck($link_path); $route = array(); if ($extracted) { if ($extracted->isExternal()) { $route['route_name'] = null; $route['route_parameters'] = array(); $route['options'] = $options; $route['url'] = $extracted->getUri(); } else { $route['route_name'] = $extracted->getRouteName(); $route['route_parameters'] = $extracted->getRouteParameters(); $route['options'] = $extracted->getOptions(); if (isset($options['query'])) { // If the querystring is stored as a string (as in D6), convert it // into an array. if (is_string($options['query'])) { parse_str($options['query'], $old_query); } else { $old_query = $options['query']; } $options['query'] = $route['options']['query'] + $old_query; unset($route['options']['query']); } $route['options'] = $route['options'] + $options; $route['url'] = null; } } return $route; }
/** * Tests the toUriString() method with internal: URIs. * * @covers ::toUriString * * @dataProvider providerTestToUriStringForInternal */ public function testToUriStringForInternal($uri, $options, $uri_string) { $url = Url::fromRoute('entity.test_entity.canonical', ['test_entity' => '1']); $this->pathValidator->expects($this->any())->method('getUrlIfValidWithoutAccessCheck')->willReturnMap([['test-entity/1', $url], ['<front>', Url::fromRoute('<front>')], ['<none>', Url::fromRoute('<none>')]]); $url = Url::fromUri($uri, $options); $this->assertSame($url->toUriString(), $uri_string); }
/** * {@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); }
/** * {@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))); } }
/** * Creates a new link in the provided shortcut set. * * @param \Drupal\shortcut\ShortcutSetInterface $shortcut_set * The shortcut set to add a link to. * @param \Symfony\Component\HttpFoundation\Request $request * The request object. * * @return \Symfony\Component\HttpFoundation\RedirectResponse * A redirect response to the front page, or the previous location. * * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException */ public function addShortcutLinkInline(ShortcutSetInterface $shortcut_set, Request $request) { $link = $request->query->get('link'); $name = $request->query->get('name'); if (parse_url($link, PHP_URL_SCHEME) === NULL && $this->pathValidator->isValid($link)) { $shortcut = $this->entityManager()->getStorage('shortcut')->create(array('title' => $name, 'shortcut_set' => $shortcut_set->id(), 'link' => array('uri' => 'internal:/' . $link))); try { $shortcut->save(); drupal_set_message($this->t('Added a shortcut for %title.', array('%title' => $shortcut->label()))); } catch (\Exception $e) { drupal_set_message($this->t('Unable to add a shortcut for %title.', array('%title' => $shortcut->label())), 'error'); } return $this->redirect('<front>'); } throw new AccessDeniedHttpException(); }
/** * Validates the form, both on the menu link edit and content menu link form. * * $form is not currently used, but passed here to match the normal form * validation method signature. * * @param array $form * A nested array form elements comprising the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */ protected function doValidate(array $form, FormStateInterface $form_state) { $extracted = $this->pathValidator->getUrlIfValid($form_state->getValue('url')); if (!$extracted) { $form_state->setErrorByName('url', $this->t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $form_state->getValue('url')))); } }
/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { $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); if ($this->aliasStorage->aliasExists($alias, $langcode, $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))); } }
/** * @param array $form * @param \Drupal\Core\Form\FormStateInterface $form_state */ public function validateForm(array &$form, FormStateInterface $form_state) { $sitemap = \Drupal::service('custom_sitemap.sitemap'); $custom_links = $sitemap->get_custom_links(); $name =& $form_state->getValue('name'); $path =& $form_state->getValue('path'); if ($form_state->getFormObject()->getFormId() == 'custom_sitemap_add_link_form' && array_key_exists($name, $custom_links)) { $form_state->setErrorByName('name', $this->t('The machine name must be unique, \'%name\' allready exists.', array('%name' => $name))); } if (!preg_match('/^[a-z0-9_]+$/', $name)) { $form_state->setErrorByName('name', $this->t('The machine name must only contain lowercase letters, numbers, and underscores.')); } if ($path[0] !== '/') { $form_state->setErrorByName('path', 'The path has to start with a slash.'); } if (!$this->path_validator->isValid(trim($path, '/'))) { $form_state->setErrorByName('path', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $path))); } }
/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { parent::validateForm($form, $form_state); // Validate and each email recipient. $recipients = explode(',', $form_state->getValue('recipients')); foreach ($recipients as &$recipient) { $recipient = trim($recipient); if (!$this->emailValidator->isValid($recipient)) { $form_state->setErrorByName('recipients', $this->t('%recipient is an invalid email address.', array('%recipient' => $recipient))); } } $form_state->setValue('recipients', $recipients); $redirect_url = $form_state->getValue('redirect'); if ($redirect_url && $this->pathValidator->isValid($redirect_url)) { if (Unicode::substr($redirect_url, 0, 1) !== '/') { $form_state->setErrorByName('redirect', $this->t('The path should start with /.')); } } }
/** * @covers ::processOutbound */ public function testOutboundPathProcessorMaxDepth() { $this->pathValidator->expects($this->any()) ->method('getUrlIfValidWithoutAccessCheck') ->willReturn(new Url('any_route')); $this->subPathautoSettings->expects($this->exactly(2)) ->method('get') ->willReturn(3); $this->aliasProcessor->expects($this->any()) ->method('processOutbound') ->will($this->returnCallback([$this, 'aliasByPathCallback'])); // Subpath shouldn't be processed since the iterations has been limited. $processed = $this->sut->processOutbound('/node/1/first/second/third/fourth'); $this->assertEquals('/node/1/first/second/third/fourth', $processed); // Subpath should be processed when the max depth doesn't exceed. $processed = $this->sut->processOutbound('/node/1/first/second/third'); $this->assertEquals('/content/first-node/first/second/third', $processed); }
/** * {@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'])) { $form_state->setErrorByName('alias', t('The alias %alias is already in use in this language.', array('%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))); } }