Skip to content

Commit

Permalink
Fixes #537 Relative paths in Cards link field (#636)
Browse files Browse the repository at this point in the history
  • Loading branch information
tadean authored Apr 7, 2021
1 parent ee5997e commit 58cbf32
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion modules/custom/az_card/config/schema/az_card.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ field.value.az_card:
type: string
label: Card Link Title
link_uri:
type: uri
type: string
label: Card Link URI
options:
type: text
Expand Down
1 change: 0 additions & 1 deletion modules/custom/az_card/css/az-card-widget.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ tr.odd .az-card-elements .form-item {
}

.az-card-elements.az-card-elements-closed .form-type--textfield,
.az-card-elements.az-card-elements-closed .form-type--url,
.az-card-elements.az-card-elements-closed .form-type--textarea,
.az-card-elements.az-card-elements-closed .filter-wrapper,
.az-card-elements.az-card-elements-closed .form-type--select,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\paragraphs\ParagraphInterface;

Expand Down Expand Up @@ -37,6 +36,13 @@ class AZCardDefaultFormatter extends FormatterBase implements ContainerFactoryPl
*/
protected $cardImageHelper;

/**
* Drupal\Core\Path\PathValidator definition.
*
* @var \Drupal\Core\Path\PathValidator
*/
protected $pathValidator;

/**
* {@inheritdoc}
*/
Expand All @@ -50,6 +56,7 @@ public static function create(ContainerInterface $container, array $configuratio

$instance->cardImageHelper = $container->get('az_card.image');
$instance->entityTypeManager = $container->get('entity_type.manager');
$instance->pathValidator = $container->get('path.validator');
return $instance;
}

Expand Down Expand Up @@ -106,10 +113,11 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
// Link.
$link_render_array = [];
if ($item->link_title || $item->link_uri) {
$link_url = $this->pathValidator->getUrlIfValid($item->link_uri);
$link_render_array = [
'#type' => 'link',
'#title' => $item->link_title ?? '',
'#url' => $item->link_uri ? Url::fromUri($item->link_uri) : '#',
'#url' => $link_url ? $link_url : '#',
'#attributes' => ['class' => ['btn', 'btn-default', 'w-100']],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel
->setLabel(t('Card Media'));
$properties['link_title'] = DataDefinition::create('string')
->setLabel(t('Card Link Title'));
$properties['link_uri'] = DataDefinition::create('uri')
$properties['link_uri'] = DataDefinition::create('string')
->setLabel(t('Card Link URI'));
$properties['options'] = MapDataDefinition::create()
->setLabel(t('Card Options'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,10 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
];

$element['link_uri'] = [
'#type' => 'url',
// Url FAPI element does not support internal paths.
'#type' => 'textfield',
'#title' => $this->t('Card Link URL'),
'#element_validate' => [[$this, 'validateCardLink']],
'#default_value' => isset($items[$delta]->link_uri) ? $items[$delta]->link_uri : NULL,
];

Expand Down Expand Up @@ -307,6 +309,26 @@ public function cardAjax(array &$form, FormStateInterface $form_state) {
return $element;
}

/**
* Form element validation handler for the 'link_url' field.
*
* Disallows saving inaccessible or untrusted URLs.
*/
public function validateCardLink(&$element, FormStateInterface $form_state, &$complete_form) {

if (!empty($element['#value'])) {
// Check to make sure the path can be found.
if ($url = $this->pathValidator->getUrlIfValid($element['#value'])) {
// Url is valid, no conversion required.
return;
}
$form_state
->setError($element, t('This link does not exist or you do not have permission to link to %path.', [
'%path' => $element['#value'],
]));
}
}

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit 58cbf32

Please sign in to comment.