diff --git a/core/lib/Drupal/Core/Url.php b/core/lib/Drupal/Core/Url.php index 6774d3c..4bfa65e 100644 --- a/core/lib/Drupal/Core/Url.php +++ b/core/lib/Drupal/Core/Url.php @@ -330,11 +330,19 @@ public function toString() { * An associative array containing all the properties of the route. */ public function toArray() { - return array( - 'route_name' => $this->routeName, - 'route_parameters' => $this->routeParameters, - 'options' => $this->options, - ); + if ($this->external) { + return array( + 'path' => $this->path, + 'options' => $this->options, + ); + } + else { + return array( + 'route_name' => $this->routeName, + 'route_parameters' => $this->routeParameters, + 'options' => $this->options, + ); + } } /** diff --git a/core/modules/link/lib/Drupal/link/Plugin/Field/FieldWidget/LinkWidget.php b/core/modules/link/lib/Drupal/link/Plugin/Field/FieldWidget/LinkWidget.php index ecf1cd5..5d9b7b8 100644 --- a/core/modules/link/lib/Drupal/link/Plugin/Field/FieldWidget/LinkWidget.php +++ b/core/modules/link/lib/Drupal/link/Plugin/Field/FieldWidget/LinkWidget.php @@ -10,8 +10,10 @@ use Drupal\Component\Utility\Url as UrlHelper; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\WidgetBase; +use Drupal\Core\Routing\MatchingRouteNotFoundException; use Drupal\Core\Url; use Drupal\link\Plugin\Field\FieldType\LinkItem; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** * Plugin implementation of the 'link' widget. @@ -44,7 +46,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen } $element['url'] = array( '#type' => 'url', - '#title' => t('URL'), + '#title' => $this->t('URL'), '#placeholder' => $this->getSetting('placeholder_url'), '#default_value' => $default_url_value, '#maxlength' => 2048, @@ -65,14 +67,14 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen $element['url']['#field_prefix'] = \Drupal::url('', array(), array('absolute' => TRUE)); } // If the field is configured to allow both internal and external paths, - // show a useful description + // show a useful description. elseif ($url_type == LinkItem::LINK_GENERIC) { - $element['url']['#description'] = t('This can be an internal Drupal path such as %add-node or an external URL such as %drupal. Enter %front to link to the front page.', array('%front' => '', '%add-node' => 'node/add', '%drupal' => 'http://drupal.org')); + $element['url']['#description'] = $this->t('This can be an internal Drupal path such as %add-node or an external URL such as %drupal. Enter %front to link to the front page.', array('%front' => '', '%add-node' => 'node/add', '%drupal' => 'http://drupal.org')); } $element['title'] = array( '#type' => 'textfield', - '#title' => t('Link text'), + '#title' => $this->t('Link text'), '#placeholder' => $this->getSetting('placeholder_title'), '#default_value' => isset($items[$delta]->title) ? $items[$delta]->title : NULL, '#maxlength' => 255, @@ -114,15 +116,15 @@ public function settingsForm(array $form, array &$form_state) { $elements['placeholder_url'] = array( '#type' => 'textfield', - '#title' => t('Placeholder for URL'), + '#title' => $this->t('Placeholder for URL'), '#default_value' => $this->getSetting('placeholder_url'), - '#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'), + '#description' => $this->t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'), ); $elements['placeholder_title'] = array( '#type' => 'textfield', - '#title' => t('Placeholder for link text'), + '#title' => $this->t('Placeholder for link text'), '#default_value' => $this->getSetting('placeholder_title'), - '#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'), + '#description' => $this->t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'), '#states' => array( 'invisible' => array( ':input[name="instance[settings][title]"]' => array('value' => DRUPAL_DISABLED), @@ -142,14 +144,14 @@ public function settingsSummary() { $placeholder_title = $this->getSetting('placeholder_title'); $placeholder_url = $this->getSetting('placeholder_url'); if (empty($placeholder_title) && empty($placeholder_url)) { - $summary[] = t('No placeholders'); + $summary[] = $this->t('No placeholders'); } else { if (!empty($placeholder_title)) { - $summary[] = t('Title placeholder: @placeholder_title', array('@placeholder_title' => $placeholder_title)); + $summary[] = $this->t('Title placeholder: @placeholder_title', array('@placeholder_title' => $placeholder_title)); } if (!empty($placeholder_url)) { - $summary[] = t('URL placeholder: @placeholder_url', array('@placeholder_url' => $placeholder_url)); + $summary[] = $this->t('URL placeholder: @placeholder_url', array('@placeholder_url' => $placeholder_url)); } } @@ -164,7 +166,7 @@ public function settingsSummary() { public function validateTitle(&$element, &$form_state, $form) { if ($element['url']['#value'] !== '' && $element['title']['#value'] === '') { $element['title']['#required'] = TRUE; - form_error($element['title'], $form_state, t('!name field is required.', array('!name' => $element['title']['#title']))); + \Drupal::formBuilder()->setError($element['title'], $form_state, $this->t('!name field is required.', array('!name' => $element['title']['#title']))); } } @@ -180,11 +182,14 @@ public function validateUrl(&$element, &$form_state, $form) { $url = Url::createFromPath($element['url']['#value']); if ($url->isExternal() && !UrlHelper::isValid($element['url']['#value'], TRUE)) { - form_error($element['url'], $form_state, t('The URL %url is not valid.', array('%url' => $element['url']['#value']))); + \Drupal::formBuilder()->setError($element['url'], $form_state, $this->t('The URL %url is not valid.', array('%url' => $element['url']['#value']))); } } - catch (\Exception $e) { - form_error($element['url'], $form_state, t('The URL %url is not valid.', array('%url' => $element['url']['#value']))); + catch (NotFoundHttpException $e) { + \Drupal::formBuilder()->setError($element['url'], $form_state, $this->t('The URL %url is not valid.', array('%url' => $element['url']['#value']))); + } + catch (MatchingRouteNotFoundException $e) { + \Drupal::formBuilder()->setError($element['url'], $form_state, $this->t('The URL %url is not valid.', array('%url' => $element['url']['#value']))); } } } @@ -201,9 +206,11 @@ public function massageFormValues(array $values, array $form, array &$form_state $value += $url->toArray(); } - catch (\Exception $e) { - // Nothing to do here, validateUrl() already emitted a form validation - // error. + catch (NotFoundHttpException $e) { + // Nothing to do here, validateUrl() emits form validation errors. + } + catch (MatchingRouteNotFoundException $e) { + // Nothing to do here, validateUrl() emits form validation errors. } } }