Loading src/Plugin/views/area/Link.php +26 −25 Original line number Diff line number Diff line Loading @@ -8,7 +8,7 @@ use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Routing\RedirectDestinationTrait; use Drupal\Core\Routing\RedirectDestination; use Drupal\Core\Url; use Drupal\views\Plugin\views\area\TokenizeAreaPluginBase; use Symfony\Component\DependencyInjection\ContainerInterface; Loading @@ -23,8 +23,6 @@ use Symfony\Component\Routing\RequestContext; */ class Link extends TokenizeAreaPluginBase { use RedirectDestinationTrait; /** * The language manager. * Loading @@ -46,6 +44,13 @@ class Link extends TokenizeAreaPluginBase { */ protected $context; /** * A helper object for handling redirect destinations. * * @var \Drupal\Core\Routing\RedirectDestination */ protected $destination; /** * Constructs a new Entity instance. * Loading @@ -61,12 +66,15 @@ class Link extends TokenizeAreaPluginBase { * The access manager. * @param \Symfony\Component\Routing\RequestContext $context * The request context. * @param \Drupal\Core\Routing\RedirectDestination $destination * A helper object for handling redirect destinations. */ public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManagerInterface $language_manager, AccessManagerInterface $access_manager, RequestContext $context) { public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManagerInterface $language_manager, AccessManagerInterface $access_manager, RequestContext $context, RedirectDestination $destination) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->languageManager = $language_manager; $this->accessManager = $access_manager; $this->context = $context; $this->destination = $destination; } /** Loading @@ -92,7 +100,8 @@ class Link extends TokenizeAreaPluginBase { $plugin_definition, $container->get('language_manager'), $container->get('access_manager'), $container->get('router.request_context') $container->get('router.request_context'), $container->get('redirect.destination') ); } Loading Loading @@ -266,7 +275,6 @@ class Link extends TokenizeAreaPluginBase { $options = $form_state->getValue('options'); // @todo validate like \Drupal\link\Plugin\Field\FieldWidget\LinkWidget $form_state->setValue('options', $options); } Loading @@ -278,13 +286,12 @@ class Link extends TokenizeAreaPluginBase { public function render($empty = FALSE) { // Note: Method adapted from the renderAsLinkmethod from // Drupal\views\Plugin\views\field\FieldPluginBase. // @phpstan-ignore-next-line if ($empty && empty($this->options['empty'])) { return []; } $options = [ 'absolute' => !empty($this->options['absolute']) ? TRUE : FALSE, // @phpstan-ignore-line 'absolute' => !empty($this->options['absolute']) ? TRUE : FALSE, 'alias' => FALSE, 'entity' => NULL, 'entity_type' => NULL, Loading Loading @@ -325,14 +332,12 @@ class Link extends TokenizeAreaPluginBase { // 'http://www.example.com'. // Only do this when flag for external has been set, $path doesn't contain // a scheme and $path doesn't have a leading /. // @phpstan-ignore-next-line if ($this->options['external'] && !parse_url($path, PHP_URL_SCHEME) && strpos($path, '/') !== 0) { // There is no scheme, add the default 'http://' to the $path. $path = "http://" . $path; } } // @phpstan-ignore-next-line if (!parse_url($path, PHP_URL_SCHEME)) { $url = Url::fromUserInput('/' . ltrim($path, '/')); } Loading @@ -344,11 +349,11 @@ class Link extends TokenizeAreaPluginBase { $path = $url->setOptions($options)->toUriString(); // @phpstan-ignore-next-line if (!empty($this->options['path_case']) && $this->options['path_case'] != 'none' && !$url->isRouted()) { $path = str_replace($this->options['path'], $this->caseTransform($this->options['path'], $this->options['path_case']), $path); } // @phpstan-ignore-next-line // @todo Where is "$url_info" defined? if (!empty($url_info['replace_spaces'])) { $path = str_replace(' ', '-', $path); } Loading @@ -357,14 +362,12 @@ class Link extends TokenizeAreaPluginBase { $url_parts = UrlHelper::parse($path); // Seriously malformed URLs may return FALSE or empty arrays. // @phpstan-ignore-next-line if (empty($url_parts)) { return $this->options['link_text']; } // If the path is empty do not build a link around the given text and return // it as is. // @phpstan-ignore-next-line if (empty($url_parts['path']) && empty($url_parts['fragment']) && empty($url_parts['url'])) { return $this->options['link_text']; } Loading Loading @@ -411,7 +414,6 @@ class Link extends TokenizeAreaPluginBase { $options['attributes']['class'] = [$class]; } // @phpstan-ignore-next-line if (!empty($this->options['rel']) && $rel = $this->tokenizeValue($this->options['rel'])) { $options['attributes']['rel'] = $rel; } Loading @@ -421,9 +423,8 @@ class Link extends TokenizeAreaPluginBase { $options['attributes']['target'] = $target; } // @phpstan-ignore-next-line if (!empty($this->options['destination'])) { $options['query'] += \Drupal::destination()->getAsArray(); $options['query'] += $this->destination->getAsArray(); } if ($this->languageManager->isMultilingual() && $this->options['language'] !== '**auto**') { Loading tests/modules/views_test_linkarea/views_test_linkarea.info.yml +0 −1 Original line number Diff line number Diff line Loading @@ -2,7 +2,6 @@ name: 'Views Test Link Area' type: module description: 'Provides default views for tests.' package: Testing version: VERSION core_version_requirement: ^9 || ^10 dependencies: - drupal:views tests/src/Kernel/Plugin/LinkAreaTest.php +4 −4 Original line number Diff line number Diff line Loading @@ -31,7 +31,7 @@ class LinkAreaTest extends ViewsKernelTestBase { 'entity_test', 'user', 'views_linkarea', 'views_test_linkarea' 'views_test_linkarea', ]; /** Loading Loading @@ -122,7 +122,7 @@ class LinkAreaTest extends ViewsKernelTestBase { 'link_text' => 'SSSSSS', ], 'SSSSSS', '/' '/', ]; $data[] = [ [ Loading @@ -130,7 +130,7 @@ class LinkAreaTest extends ViewsKernelTestBase { 'link_text' => '<b>Pass</b>', ], 'Pass', '/user/password' '/user/password', ]; $data[] = [ [ Loading @@ -138,7 +138,7 @@ class LinkAreaTest extends ViewsKernelTestBase { 'link_text' => '<b>Pass</b>', ], 'Pass', '/entity_test/1' '/entity_test/1', ]; return $data; } Loading views_linkarea.info.yml +0 −1 Original line number Diff line number Diff line Loading @@ -2,7 +2,6 @@ name: 'Views Link Area' description: 'A Views area plugin to allow links as headers and footers for views.' type: module core_version_requirement: ^9 || ^10 version: VERSION package: 'Views' dependencies: - drupal:views Loading
src/Plugin/views/area/Link.php +26 −25 Original line number Diff line number Diff line Loading @@ -8,7 +8,7 @@ use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Routing\RedirectDestinationTrait; use Drupal\Core\Routing\RedirectDestination; use Drupal\Core\Url; use Drupal\views\Plugin\views\area\TokenizeAreaPluginBase; use Symfony\Component\DependencyInjection\ContainerInterface; Loading @@ -23,8 +23,6 @@ use Symfony\Component\Routing\RequestContext; */ class Link extends TokenizeAreaPluginBase { use RedirectDestinationTrait; /** * The language manager. * Loading @@ -46,6 +44,13 @@ class Link extends TokenizeAreaPluginBase { */ protected $context; /** * A helper object for handling redirect destinations. * * @var \Drupal\Core\Routing\RedirectDestination */ protected $destination; /** * Constructs a new Entity instance. * Loading @@ -61,12 +66,15 @@ class Link extends TokenizeAreaPluginBase { * The access manager. * @param \Symfony\Component\Routing\RequestContext $context * The request context. * @param \Drupal\Core\Routing\RedirectDestination $destination * A helper object for handling redirect destinations. */ public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManagerInterface $language_manager, AccessManagerInterface $access_manager, RequestContext $context) { public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManagerInterface $language_manager, AccessManagerInterface $access_manager, RequestContext $context, RedirectDestination $destination) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->languageManager = $language_manager; $this->accessManager = $access_manager; $this->context = $context; $this->destination = $destination; } /** Loading @@ -92,7 +100,8 @@ class Link extends TokenizeAreaPluginBase { $plugin_definition, $container->get('language_manager'), $container->get('access_manager'), $container->get('router.request_context') $container->get('router.request_context'), $container->get('redirect.destination') ); } Loading Loading @@ -266,7 +275,6 @@ class Link extends TokenizeAreaPluginBase { $options = $form_state->getValue('options'); // @todo validate like \Drupal\link\Plugin\Field\FieldWidget\LinkWidget $form_state->setValue('options', $options); } Loading @@ -278,13 +286,12 @@ class Link extends TokenizeAreaPluginBase { public function render($empty = FALSE) { // Note: Method adapted from the renderAsLinkmethod from // Drupal\views\Plugin\views\field\FieldPluginBase. // @phpstan-ignore-next-line if ($empty && empty($this->options['empty'])) { return []; } $options = [ 'absolute' => !empty($this->options['absolute']) ? TRUE : FALSE, // @phpstan-ignore-line 'absolute' => !empty($this->options['absolute']) ? TRUE : FALSE, 'alias' => FALSE, 'entity' => NULL, 'entity_type' => NULL, Loading Loading @@ -325,14 +332,12 @@ class Link extends TokenizeAreaPluginBase { // 'http://www.example.com'. // Only do this when flag for external has been set, $path doesn't contain // a scheme and $path doesn't have a leading /. // @phpstan-ignore-next-line if ($this->options['external'] && !parse_url($path, PHP_URL_SCHEME) && strpos($path, '/') !== 0) { // There is no scheme, add the default 'http://' to the $path. $path = "http://" . $path; } } // @phpstan-ignore-next-line if (!parse_url($path, PHP_URL_SCHEME)) { $url = Url::fromUserInput('/' . ltrim($path, '/')); } Loading @@ -344,11 +349,11 @@ class Link extends TokenizeAreaPluginBase { $path = $url->setOptions($options)->toUriString(); // @phpstan-ignore-next-line if (!empty($this->options['path_case']) && $this->options['path_case'] != 'none' && !$url->isRouted()) { $path = str_replace($this->options['path'], $this->caseTransform($this->options['path'], $this->options['path_case']), $path); } // @phpstan-ignore-next-line // @todo Where is "$url_info" defined? if (!empty($url_info['replace_spaces'])) { $path = str_replace(' ', '-', $path); } Loading @@ -357,14 +362,12 @@ class Link extends TokenizeAreaPluginBase { $url_parts = UrlHelper::parse($path); // Seriously malformed URLs may return FALSE or empty arrays. // @phpstan-ignore-next-line if (empty($url_parts)) { return $this->options['link_text']; } // If the path is empty do not build a link around the given text and return // it as is. // @phpstan-ignore-next-line if (empty($url_parts['path']) && empty($url_parts['fragment']) && empty($url_parts['url'])) { return $this->options['link_text']; } Loading Loading @@ -411,7 +414,6 @@ class Link extends TokenizeAreaPluginBase { $options['attributes']['class'] = [$class]; } // @phpstan-ignore-next-line if (!empty($this->options['rel']) && $rel = $this->tokenizeValue($this->options['rel'])) { $options['attributes']['rel'] = $rel; } Loading @@ -421,9 +423,8 @@ class Link extends TokenizeAreaPluginBase { $options['attributes']['target'] = $target; } // @phpstan-ignore-next-line if (!empty($this->options['destination'])) { $options['query'] += \Drupal::destination()->getAsArray(); $options['query'] += $this->destination->getAsArray(); } if ($this->languageManager->isMultilingual() && $this->options['language'] !== '**auto**') { Loading
tests/modules/views_test_linkarea/views_test_linkarea.info.yml +0 −1 Original line number Diff line number Diff line Loading @@ -2,7 +2,6 @@ name: 'Views Test Link Area' type: module description: 'Provides default views for tests.' package: Testing version: VERSION core_version_requirement: ^9 || ^10 dependencies: - drupal:views
tests/src/Kernel/Plugin/LinkAreaTest.php +4 −4 Original line number Diff line number Diff line Loading @@ -31,7 +31,7 @@ class LinkAreaTest extends ViewsKernelTestBase { 'entity_test', 'user', 'views_linkarea', 'views_test_linkarea' 'views_test_linkarea', ]; /** Loading Loading @@ -122,7 +122,7 @@ class LinkAreaTest extends ViewsKernelTestBase { 'link_text' => 'SSSSSS', ], 'SSSSSS', '/' '/', ]; $data[] = [ [ Loading @@ -130,7 +130,7 @@ class LinkAreaTest extends ViewsKernelTestBase { 'link_text' => '<b>Pass</b>', ], 'Pass', '/user/password' '/user/password', ]; $data[] = [ [ Loading @@ -138,7 +138,7 @@ class LinkAreaTest extends ViewsKernelTestBase { 'link_text' => '<b>Pass</b>', ], 'Pass', '/entity_test/1' '/entity_test/1', ]; return $data; } Loading
views_linkarea.info.yml +0 −1 Original line number Diff line number Diff line Loading @@ -2,7 +2,6 @@ name: 'Views Link Area' description: 'A Views area plugin to allow links as headers and footers for views.' type: module core_version_requirement: ^9 || ^10 version: VERSION package: 'Views' dependencies: - drupal:views