Ejemplo n.º 1
1
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if (isset($value)) {
         $url_is_valid = TRUE;
         /** @var $link_item \Drupal\link\LinkItemInterface */
         $link_item = $value;
         $link_type = $link_item->getFieldDefinition()->getSetting('link_type');
         $url_string = $link_item->url;
         // Validate the url property.
         if ($url_string !== '') {
             try {
                 // @todo This shouldn't be needed, but massageFormValues() may not
                 //   run.
                 $parsed_url = UrlHelper::parse($url_string);
                 $url = Url::createFromPath($parsed_url['path']);
                 if ($url->isExternal() && !UrlHelper::isValid($url_string, TRUE)) {
                     $url_is_valid = FALSE;
                 } elseif ($url->isExternal() && !($link_type & LinkItemInterface::LINK_EXTERNAL)) {
                     $url_is_valid = FALSE;
                 }
             } catch (NotFoundHttpException $e) {
                 $url_is_valid = FALSE;
             } catch (MatchingRouteNotFoundException $e) {
                 $url_is_valid = FALSE;
             } catch (ParamNotConvertedException $e) {
                 $url_is_valid = FALSE;
             }
         }
         if (!$url_is_valid) {
             $this->context->addViolation($this->message, array('%url' => $url_string));
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Tests the createFromPath method.
  *
  * @covers ::createFromPath()
  * @covers ::setExternal()
  */
 public function testCreateFromPath()
 {
     $url = Url::createFromPath($this->path);
     $this->assertInstanceOf('Drupal\\Core\\Url', $url);
     $this->assertTrue($url->isExternal());
     return $url;
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function getUrlObject()
 {
     if ($route_name = $this->getRouteName()) {
         $url = new Url($route_name, $this->getRouteParameters(), $this->getOptions());
     } else {
         $path = $this->getUrl();
         if (isset($path)) {
             $url = Url::createFromPath($path);
         } else {
             $url = new Url('<front>');
         }
     }
     return $url;
 }
Ejemplo n.º 4
0
 /**
  * Breaks up a user-entered URL or path into all the relevant parts.
  *
  * @param string $url
  *   The user-entered URL or path.
  *
  * @return array
  *   The extracted parts.
  */
 protected function extractUrl($url)
 {
     $extracted = UrlHelper::parse($url);
     $external = UrlHelper::isExternal($url);
     if ($external) {
         $extracted['url'] = $extracted['path'];
         $extracted['route_name'] = NULL;
         $extracted['route_parameters'] = array();
     } else {
         $extracted['url'] = '';
         // If the path doesn't match a Drupal path, the route should end up empty.
         $extracted['route_name'] = NULL;
         $extracted['route_parameters'] = array();
         try {
             // Find the route_name.
             $normal_path = $this->pathAliasManager->getPathByAlias($extracted['path']);
             $url_obj = Url::createFromPath($normal_path);
             $extracted['route_name'] = $url_obj->getRouteName();
             $extracted['route_parameters'] = $url_obj->getRouteParameters();
         } catch (MatchingRouteNotFoundException $e) {
             // The path doesn't match a Drupal path.
         } catch (ParamNotConvertedException $e) {
             // A path like node/99 matched a route, but the route parameter was
             // invalid (e.g. node with ID 99 does not exist).
         }
     }
     return $extracted;
 }
Ejemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function massageFormValues(array $values, array $form, array &$form_state)
 {
     foreach ($values as &$value) {
         if (!empty($value['url'])) {
             try {
                 $parsed_url = UrlHelper::parse($value['url']);
                 // If internal links are supported, look up whether the given value is
                 // a path alias and store the system path instead.
                 if ($this->supportsInternalLinks() && !UrlHelper::isExternal($value['url'])) {
                     $parsed_url['path'] = \Drupal::service('path.alias_manager')->getPathByAlias($parsed_url['path']);
                 }
                 $url = Url::createFromPath($parsed_url['path']);
                 $url->setOption('query', $parsed_url['query']);
                 $url->setOption('fragment', $parsed_url['fragment']);
                 $url->setOption('attributes', $value['attributes']);
                 $value += $url->toArray();
                 // Reset the URL value to contain only the path.
                 $value['url'] = $parsed_url['path'];
             } catch (NotFoundHttpException $e) {
                 // Nothing to do here, LinkTypeConstraintValidator emits errors.
             } catch (MatchingRouteNotFoundException $e) {
                 // Nothing to do here, LinkTypeConstraintValidator emits errors.
             } catch (ParamNotConvertedException $e) {
                 // Nothing to do here, LinkTypeConstraintValidator emits errors.
             }
         }
     }
     return $values;
 }
Ejemplo n.º 6
0
 /**
  * Tests the getRouteParameters() with an external URL.
  *
  * @covers ::getRouteParameters
  * @expectedException \UnexpectedValueException
  */
 public function testGetRouteParametersWithExternalUrl()
 {
     $url = Url::createFromPath('http://example.com');
     $url->getRouteParameters();
 }
Ejemplo n.º 7
0
 /**
  * {@inheritdoc}
  */
 public function preSave(EntityStorageInterface $storage)
 {
     parent::preSave($storage);
     $url = Url::createFromPath($this->path->value);
     $this->setRouteName($url->getRouteName());
     $this->setRouteParams($url->getRouteParameters());
 }
Ejemplo n.º 8
0
 /**
  * {@inheritdoc}
  */
 public function preSave(EntityStorageInterface $storage)
 {
     parent::preSave($storage);
     // This is the easiest way to handle the unique internal path '<front>',
     // since a path marked as external does not need to match a route.
     $this->external = UrlHelper::isExternal($this->link_path) || $this->link_path == '<front>' ? 1 : 0;
     // Try to find a parent link. If found, assign it and derive its menu.
     $parent = $this->findParent($storage);
     if ($parent) {
         $this->plid = $parent->id();
         $this->menu_name = $parent->menu_name;
     } else {
         $this->plid = 0;
     }
     // Directly fill parents for top-level links.
     if ($this->plid == 0) {
         $this->p1 = $this->id();
         for ($i = 2; $i <= MENU_MAX_DEPTH; $i++) {
             $parent_property = "p{$i}";
             $this->{$parent_property} = 0;
         }
         $this->depth = 1;
     } else {
         if ($this->has_children && $this->original) {
             $limit = MENU_MAX_DEPTH - $storage->findChildrenRelativeDepth($this->original) - 1;
         } else {
             $limit = MENU_MAX_DEPTH - 1;
         }
         if ($parent->depth > $limit) {
             return FALSE;
         }
         $this->depth = $parent->depth + 1;
         $this->setParents($parent);
     }
     // Need to check both plid and menu_name, since plid can be 0 in any menu.
     if (isset($this->original) && ($this->plid != $this->original->plid || $this->menu_name != $this->original->menu_name)) {
         $storage->moveChildren($this);
     }
     // Find the route_name.
     if (!$this->external && !isset($this->route_name)) {
         $url = Url::createFromPath($this->link_path);
         $this->route_name = $url->getRouteName();
         $this->route_parameters = $url->getRouteParameters();
     } elseif (empty($this->link_path)) {
         $this->link_path = \Drupal::urlGenerator()->getPathFromRoute($this->route_name, $this->route_parameters);
     }
 }
Ejemplo n.º 9
0
 /**
  * {@inheritdoc}
  */
 public function getUrlObject($title_attribute = TRUE)
 {
     $options = $this->getOptions();
     $description = $this->getDescription();
     if ($title_attribute && $description) {
         $options['attributes']['title'] = $description;
     }
     if (empty($this->pluginDefinition['url'])) {
         return new Url($this->pluginDefinition['route_name'], $this->pluginDefinition['route_parameters'], $options);
     } else {
         $url = Url::createFromPath($this->pluginDefinition['url']);
         $url->setOptions($options);
         return $url;
     }
 }
Ejemplo n.º 10
0
 /**
  * Entity URI callback.
  */
 public static function buildUri(ItemInterface $item)
 {
     return Url::createFromPath($item->getLink());
 }
Ejemplo n.º 11
0
 /**
  * Builds the \Drupal\Core\Url object for a link field item.
  *
  * @param \Drupal\link\LinkItemInterface $item
  *   The link field item being rendered.
  *
  * @return \Drupal\Core\Url
  *   An Url object.
  */
 protected function buildUrl(LinkItemInterface $item)
 {
     $settings = $this->getSettings();
     $options = $item->options;
     // Add optional 'rel' attribute to link options.
     if (!empty($settings['rel'])) {
         $options['attributes']['rel'] = $settings['rel'];
     }
     // Add optional 'target' attribute to link options.
     if (!empty($settings['target'])) {
         $options['attributes']['target'] = $settings['target'];
     }
     if ($item->isExternal()) {
         $url = Url::createFromPath($item->url);
         $url->setOptions($options);
     } else {
         $url = new Url($item->route_name, (array) $item->route_parameters, (array) $options);
     }
     return $url;
 }
Ejemplo n.º 12
0
 /**
  * Tests the generateFromUrl() method with an external URL.
  *
  * The set_active_class option is set to TRUE to ensure this does not cause
  * an error together with an external URL.
  *
  * @covers ::generateFromUrl()
  */
 public function testGenerateFromUrlExternal()
 {
     $this->urlGenerator->expects($this->once())->method('generateFromPath')->with('http://drupal.org', array('set_active_class' => TRUE, 'external' => TRUE) + $this->defaultOptions)->will($this->returnArgument(0));
     $this->moduleHandler->expects($this->once())->method('alter')->with('link', $this->isType('array'));
     $url = Url::createFromPath('http://drupal.org');
     $url->setUrlGenerator($this->urlGenerator);
     $url->setOption('set_active_class', TRUE);
     $result = $this->linkGenerator->generateFromUrl('Drupal', $url);
     $this->assertTag(array('tag' => 'a', 'attributes' => array('href' => 'http://drupal.org'), 'content' => 'Drupal'), $result);
 }