From adc876c338eb3cf54710f905591d53728a296dee Mon Sep 17 00:00:00 2001 From: Johnathan Bamber Date: Thu, 4 Aug 2022 01:18:12 -0400 Subject: [PATCH 1/4] Added core_version_requirement to .info.yml. --- aluminum_blocks.info.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aluminum_blocks.info.yml b/aluminum_blocks.info.yml index 5ea6b6d..334d8c9 100644 --- a/aluminum_blocks.info.yml +++ b/aluminum_blocks.info.yml @@ -1,8 +1,8 @@ name: Aluminum Blocks type: module description: 'Creates some basic user-configurable blocks that are generally useful for most sites.' -version: 8.x-1.x -core: 8.x +version: 3.x +core_version_requirement: ^8 || ^9 package: Aluminum dependencies: - aluminum From 75203200030f4d8a72595aa183714ff6d53d74a2 Mon Sep 17 00:00:00 2001 From: Johnathan Bamber Date: Fri, 5 Aug 2022 10:23:36 -0400 Subject: [PATCH 2/4] Fix reference to path.alias_manager service with the new path_alias.manager. --- src/Plugin/Block/AluminumLinkBlock.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin/Block/AluminumLinkBlock.php b/src/Plugin/Block/AluminumLinkBlock.php index 8dfbdaa..0e30f35 100644 --- a/src/Plugin/Block/AluminumLinkBlock.php +++ b/src/Plugin/Block/AluminumLinkBlock.php @@ -93,7 +93,7 @@ protected function getUrl() { $url_object = \Drupal::service('path.validator')->getUrlIfValid($fake_request->getRequestUri()); if ($url_object) { - $back_url = \Drupal::service('path.alias_manager')->getAliasByPath('/'.$url_object->getInternalPath()); + $back_url = \Drupal::service('path_alias.manager')->getAliasByPath('/'.$url_object->getInternalPath()); } else { $back_url = '/'; } From 1bab217aa57e910514dae00c780493c5b0a9876b Mon Sep 17 00:00:00 2001 From: Scott Sawyer Date: Fri, 23 Aug 2024 01:15:10 -0400 Subject: [PATCH 3/4] Fixes #4 - Adds Drupal 10 compatibility --- aluminum_blocks.info.yml | 8 +- aluminum_blocks.libraries.yml | 4 + aluminum_blocks.module | 49 +++-- src/Plugin/Block/AluminumAccountLinkBlock.php | 130 ++++++++++++-- src/Plugin/Block/AluminumAddThisBlock.php | 12 +- src/Plugin/Block/AluminumBlockBase.php | 63 ++++++- src/Plugin/Block/AluminumContentBlock.php | 105 +++++++++-- src/Plugin/Block/AluminumCopyrightBlock.php | 6 +- .../Block/AluminumDropdownLinksBlock.php | 13 +- src/Plugin/Block/AluminumFollowBlock.php | 49 ++++- .../Block/AluminumFollowCustomBlock.php | 3 - src/Plugin/Block/AluminumIconBlock.php | 10 +- src/Plugin/Block/AluminumIframeBlock.php | 26 ++- src/Plugin/Block/AluminumLinkBlock.php | 168 +++++++++++++++--- src/Plugin/Block/AluminumLinkListBlock.php | 14 +- src/Plugin/Block/AluminumOfficeInfoBlock.php | 29 +-- src/Plugin/Block/AluminumPhoneNumberBlock.php | 17 +- .../Block/AluminumPhoneNumberListBlock.php | 33 ++-- src/Plugin/Block/AluminumScriptTagBlock.php | 9 +- src/Plugin/Block/AluminumSnippetBlock.php | 6 +- 20 files changed, 594 insertions(+), 160 deletions(-) diff --git a/aluminum_blocks.info.yml b/aluminum_blocks.info.yml index 334d8c9..8a357ec 100644 --- a/aluminum_blocks.info.yml +++ b/aluminum_blocks.info.yml @@ -2,9 +2,9 @@ name: Aluminum Blocks type: module description: 'Creates some basic user-configurable blocks that are generally useful for most sites.' version: 3.x -core_version_requirement: ^8 || ^9 +core_version_requirement: ^9 || ^10 package: Aluminum dependencies: - - aluminum - - aluminum_storage - - block + - aluminum:aluminum + - aluminum_storage:aluminum_storage + - block:block diff --git a/aluminum_blocks.libraries.yml b/aluminum_blocks.libraries.yml index 2854729..beef117 100644 --- a/aluminum_blocks.libraries.yml +++ b/aluminum_blocks.libraries.yml @@ -6,6 +6,10 @@ aluminum_iframe: js: js/iframeResizer.min.js: {} js/aluminum-iframe.js: {} + dependencies: + - core/jquery + - core/drupal + aluminum_addthis: version: 1.0.0 js: {} diff --git a/aluminum_blocks.module b/aluminum_blocks.module index bbb4989..129d686 100644 --- a/aluminum_blocks.module +++ b/aluminum_blocks.module @@ -14,9 +14,13 @@ function aluminum_blocks_help($route_name, RouteMatchInterface $route_match) { switch ($route_name) { case 'help.page.aluminum_blocks': return check_markup(file_get_contents(dirname(__FILE__) . "/README.txt")); + break; } } +/** + * Implements hook_theme(). + */ function aluminum_blocks_theme($existing, $type, $theme, $path) { return [ 'aluminum_follow_list' => [ @@ -29,28 +33,52 @@ function aluminum_blocks_theme($existing, $type, $theme, $path) { 'variables' => ['link_list' => NULL, 'button_text' => NULL], ], 'aluminum_phone_number' => [ - 'variables' => ['phone_number' => NULL, 'url' => NULL] + 'variables' => ['phone_number' => NULL, 'url' => NULL], ], 'aluminum_phone_number_list' => [ 'variables' => ['list' => NULL], ], 'aluminum_content' => [ - 'variables' => ['entity_Type' => NULL, 'entity_id' => NULL, 'view_mode' => NULL] + 'variables' => [ + 'entity_Type' => NULL, + 'entity_id' => NULL, + 'view_mode' => NULL, + ], ], 'aluminum_office_info' => [ - 'variables' => ['address' => NULL, 'phone_list' => NULL, 'office_hours' => NULL] + 'variables' => [ + 'address' => NULL, + 'phone_list' => NULL, + 'office_hours' => NULL, + ], ], 'aluminum_icon' => [ - 'variables' => ['icon' => NULL, 'output_as_link' => NULL, 'link_url' => NULL] + 'variables' => [ + 'icon' => NULL, + 'output_as_link' => NULL, + 'link_url' => NULL, + ], ], 'aluminum_link' => [ - 'variables' => ['title' => NULL, 'url' => NULL, 'classes' => []], + 'variables' => [ + 'title' => NULL, + 'url' => NULL, + 'classes' => [], + ], ], 'aluminum_script_tag' => [ - 'variables' => ['wrapper_classes' => '', 'wrapper_id' => '', 'script' => NULL], + 'variables' => [ + 'wrapper_classes' => '', + 'wrapper_id' => '', + 'script' => NULL, + ], ], 'aluminum_snippet' => [ - 'variables' => ['wrapper_classes' => '', 'wrapper_id' => '', 'snippet' => ''], + 'variables' => [ + 'wrapper_classes' => '', + 'wrapper_id' => '', + 'snippet' => '', + ], ], 'aluminum_iframe' => [ 'variables' => [ @@ -68,10 +96,3 @@ function aluminum_blocks_theme($existing, $type, $theme, $path) { ], ]; } - -/** - * Implements hook_library_info_alter(). - */ -function aluminum_blocks_library_info_alter(&$libraries, $extension) { - -} diff --git a/src/Plugin/Block/AluminumAccountLinkBlock.php b/src/Plugin/Block/AluminumAccountLinkBlock.php index 68d5547..ed48c40 100644 --- a/src/Plugin/Block/AluminumAccountLinkBlock.php +++ b/src/Plugin/Block/AluminumAccountLinkBlock.php @@ -1,10 +1,14 @@ account = $account; + $this->routeMatch = $route_match; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */ + $config_factory = $container->get('config.factory'); + /** @var \Drupal\Core\Session\AccountProxyInterface $account */ + $account = $container->get('current_user'); + /** @var \Drupal\Core\Routing\RouteMatchInterface $route_match */ + $route_match = $container->get('current_route_match'); + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $config_factory, + $account, + $route_match + ); + } + /** * {@inheritdoc} */ - public function getOptions() { + public function getOptions(): array { $options = []; $options['login_text'] = [ @@ -38,33 +99,63 @@ public function getOptions() { return $options; } - protected function getLinkTitle() { + /** + * Get link title. + * + * @return array|string + * The link title. + */ + protected function getLinkTitle(): array|string { return $this->getOptionValue(($this->isLoggedIn() ? 'account_text' : 'login_text')); } - protected function getLinkUrl() { + /** + * Get link url. + * + * @return \Drupal\Core\Url + * The url. + */ + protected function getLinkUrl(): Url { return Url::fromRoute(($this->isLoggedIn() ? 'user.page' : 'user.login')); } - protected function getLinkClassFragment() { + /** + * Get link fragment. + * + * @return string + * Link class fragment. + */ + protected function getLinkClassFragment(): string { return $this->isLoggedIn() ? 'account' : 'login'; } - protected function isLoggedIn() { - return \Drupal::currentUser()->isAuthenticated(); + /** + * Is user logged in. + * + * @return bool + * Whether the current user is authenticated. + */ + protected function isLoggedIn(): bool { + return $this->account->isAuthenticated(); } - protected function isActiveTrail() { - $currentUrl = Url::fromRouteMatch(\Drupal::routeMatch())->getInternalPath(); + /** + * Is active trail. + * + * @return bool + * Whether there is an active trail. + */ + protected function isActiveTrail(): bool { + $currentUrl = Url::fromRouteMatch($this->routeMatch)->getInternalPath(); $url = $this->getLinkUrl()->getInternalPath(); - return (strpos($currentUrl, $url) === 0); + return (str_starts_with($currentUrl, $url)); } /** * {@inheritdoc} */ - public function build() { + public function build(): array { $classes = [ 'aluminum-account-link', 'aluminum-account-link--' . $this->getLinkClassFragment(), @@ -82,10 +173,17 @@ public function build() { ]; } - public function getCacheContexts() { - //if you depends on \Drupal::routeMatch() - //you must set context of this block with 'route' context tag. - //Every new route this block will rebuild + /** + * Get cache contexts. + * + * @return array|string[] + * An array of cache contexts. + */ + public function getCacheContexts(): array { + // If you depend on \Drupal::routeMatch() + // you must set context of this block with 'route' context tag. + // Every new route this block will rebuild return Cache::mergeContexts(parent::getCacheContexts(), array('route')); } + } diff --git a/src/Plugin/Block/AluminumAddThisBlock.php b/src/Plugin/Block/AluminumAddThisBlock.php index a9b0d07..854a11f 100644 --- a/src/Plugin/Block/AluminumAddThisBlock.php +++ b/src/Plugin/Block/AluminumAddThisBlock.php @@ -13,10 +13,11 @@ * ) */ class AluminumAddThisBlock extends AluminumBlockBase { + /** * {@inheritdoc} */ - public function build() { + public function build(): array { $build = [ '#markup' => '
', '#attached' => ['library' => ['aluminum_blocks/aluminum_addthis']], @@ -27,10 +28,17 @@ public function build() { return $build; } - public function getCacheContexts() { + /** + * Get cache contexts. + * + * @return array|string[] + * An array of cache contexts. + */ + public function getCacheContexts(): array { //if you depends on \Drupal::routeMatch() //you must set context of this block with 'route' context tag. //Every new route this block will rebuild return Cache::mergeContexts(parent::getCacheContexts(), array('route')); } + } diff --git a/src/Plugin/Block/AluminumBlockBase.php b/src/Plugin/Block/AluminumBlockBase.php index 909a021..eff0991 100644 --- a/src/Plugin/Block/AluminumBlockBase.php +++ b/src/Plugin/Block/AluminumBlockBase.php @@ -4,22 +4,68 @@ use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockPluginInterface; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; + +/** + * Provides an abstract base class for Aluminum Blocks. + */ +abstract class AluminumBlockBase extends BlockBase implements BlockPluginInterface, ContainerFactoryPluginInterface { -abstract class AluminumBlockBase extends BlockBase implements BlockPluginInterface { /** * Optionally override this to manually set an aluminum_id for this block. * * @var string */ - var $aluminum_id = ''; + protected $aluminum_id = ''; + + /** + * ConfigFactoryInterface definition. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + + /** + * Constructs a new AluminumBlockBase object. + * + * @param array $configuration + * The block plugin configuration. + * @param string $plugin_id + * The block plugin id. + * @param mixed $plugin_definition + * The plugin definition. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The config factory. + */ + public function __construct(array $configuration, string $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) { + $this->configFactory = $config_factory; + parent::__construct($configuration, $plugin_id, $plugin_definition); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */ + $config_factory = $container->get('config.factory'); + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $config_factory + ); + } /** * Override to specify configuration options * * @return array + * An array of options. */ - public function getOptions() { + public function getOptions(): array { return []; } @@ -46,7 +92,7 @@ public function getOptionValue($option_name, $replace_tokens = FALSE) { $options = $this->getOptions(); - $default = isset($options[$option_name]['#default_value']) ? $options[$option_name]['#default_value'] : ''; + $default = $options[$option_name]['#default_value'] ?? ''; if (isset($config[$option_name]['#type']) && $config[$option_name]['#type'] === 'formatted_text') { $default = [ @@ -55,7 +101,7 @@ public function getOptionValue($option_name, $replace_tokens = FALSE) { ]; } - $value = isset($config[$option_name]) ? $config[$option_name] : $default; + $value = $config[$option_name] ?? $default; if ($replace_tokens) { $value = \Drupal::token()->replace($value); @@ -78,9 +124,9 @@ public function blockForm($form, FormStateInterface $form_state) { '#title' => $this->t(ucfirst(str_replace('_', ' ', $option_name))), ]; - $default = isset($option['#default_value']) ? $option['#default_value'] : ''; + $default = $option['#default_value'] ?? ''; - $option['#default_value'] = isset($config[$option_name]) ? $config[$option_name] : $default; + $option['#default_value'] = $config[$option_name] ?? $default; $form[$option_name] = $option; } @@ -101,7 +147,7 @@ public function blockSubmit($form, FormStateInterface $form_state) { * {@inheritdoc} */ public function defaultConfiguration() { - $default_config = \Drupal::config('aluminum_blocks.settings'); + $default_config = $this->configFactory->get('aluminum_blocks.settings'); $values = []; @@ -111,4 +157,5 @@ public function defaultConfiguration() { return $values; } + } diff --git a/src/Plugin/Block/AluminumContentBlock.php b/src/Plugin/Block/AluminumContentBlock.php index af81c8c..d546c83 100644 --- a/src/Plugin/Block/AluminumContentBlock.php +++ b/src/Plugin/Block/AluminumContentBlock.php @@ -2,10 +2,13 @@ namespace Drupal\aluminum_blocks\Plugin\Block; -use Drupal\Core\Annotation\Translation; -use Drupal\Core\Block\Annotation\Block; +use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\FieldableEntityInterface; +use Drupal\Core\Path\CurrentPathStack; use Drupal\Core\Url; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a 'Content' block @@ -16,10 +19,67 @@ * ) */ class AluminumContentBlock extends AluminumBlockBase { + + /** + * EntityTypeManagerInterface definition. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + + /** + * CurrentPathStack definition. + * + * @var \Drupal\Core\Path\CurrentPathStack + */ + protected $currentPathStack; + + /** + * Constructs a new AluminumBlockBase object. + * + * @param array $configuration + * The block plugin configuration. + * @param string $plugin_id + * The block plugin id. + * @param mixed $plugin_definition + * The plugin definition. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The config factory. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager. + * @param \Drupal\Core\Path\CurrentPathStack $current_path_stack + * The current path stack. + */ + public function __construct(array $configuration, string $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, CurrentPathStack $current_path_stack) { + parent::__construct($configuration, $plugin_id, $plugin_definition, $config_factory); + $this->entityTypeManager = $entity_type_manager; + $this->currentPathStack = $current_path_stack; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */ + $config_factory = $container->get('config.factory'); + /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */ + $entity_type_manager = $container->get('entity_type.manager'); + /** @var \Drupal\Core\Path\CurrentPathStack $current_path_stack */ + $current_path_stack = $container->get('path.current'); + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $config_factory, + $entity_type_manager, + $current_path_stack + ); + } + /** * {@inheritdoc} */ - public function getOptions() { + public function getOptions(): array { return [ 'entity_type' => [ '#type' => 'textfield', @@ -45,7 +105,7 @@ public function getOptions() { /** * {@inheritdoc} */ - public function build() { + public function build(): array { $build = [ '#cache' => [ 'max-age' => 0, @@ -65,8 +125,9 @@ public function build() { * Build a view using a view builder for the configured entity and view mode * * @return array + * The entity view. */ - protected function getEntityView() { + protected function getEntityView(): array { $entity_view = []; $entity = $this->loadEntity(); @@ -74,7 +135,7 @@ protected function getEntityView() { $view_mode = $this->getOptionValue('view_mode') ?: 'full'; if ($this->hasContent($entity, $view_mode)) { - $entity_view = \Drupal::entityTypeManager() + $entity_view = $this->entityTypeManager ->getViewBuilder($entity->getEntityTypeId()) ->view($entity, $view_mode); } @@ -84,14 +145,20 @@ protected function getEntityView() { } /** + * If the entity has content. + * * Checks of the entity has content in any of the fields displayed on the * provided view mode. * * @param \Drupal\Core\Entity\FieldableEntityInterface $entity - * @param $view_mode + * The entity. + * @param string $view_mode + * The view mode. + * * @return bool + * Whether there is content. */ - protected function hasContent(FieldableEntityInterface $entity, $view_mode) { + protected function hasContent(FieldableEntityInterface $entity, string $view_mode): bool { $has_content = FALSE; foreach ($this->getDisplayFields($entity, $view_mode) as $field_name => $field_settings) { @@ -108,14 +175,18 @@ protected function hasContent(FieldableEntityInterface $entity, $view_mode) { * Gets the fields that are configured on the provided view mode. * * @param \Drupal\Core\Entity\FieldableEntityInterface $entity - * @param $view_mode + * The entity. + * @param string $view_mode + * The view mode. + * * @return array + * An array of fields. */ protected function getDisplayFields(FieldableEntityInterface $entity, $view_mode) { $fields = []; /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */ - $display = \Drupal::entityTypeManager() + $display = $this->entityTypeManager ->getStorage('entity_view_display') ->load($entity->getEntityTypeId() . '.' . $entity->bundle() . '.' . $view_mode); @@ -136,7 +207,7 @@ protected function getDisplayFields(FieldableEntityInterface $entity, $view_mode * @return \Drupal\Core\Entity\FieldableEntityInterface|null * The loaded entity, or NULL */ - protected function loadEntity() { + protected function loadEntity(): EntityInterface|FieldableEntityInterface|null { $entity = NULL; $entityType = $this->getOptionValue('entity_type'); @@ -149,7 +220,7 @@ protected function loadEntity() { if (!empty($entityId)) { /** @var FieldableEntityInterface $entity */ - $entity = \Drupal::entityTypeManager()->getStorage($entityType)->load($entityId); + $entity = $this->entityTypeManager->getStorage($entityType)->load($entityId); } } @@ -166,7 +237,7 @@ protected function loadEntity() { * The entity object from the current request URI, or NULL */ protected function loadCurrentEntity($entityType = NULL) { - $path = \Drupal::service('path.current')->getPath(); + $path = $this->currentPathStack->getPath(); $url = Url::fromUri('internal:' . $path); @@ -178,18 +249,20 @@ protected function loadCurrentEntity($entityType = NULL) { if (empty($entityType)) { if (isset($params['entity_type'])) { $entityType = $params['entity_type']; - } else { + } + else { $entityType = key($params); } } if (!empty($entityType)) { - $param = isset($params['entity']) ? $params['entity'] : $params[$entityType]; - $entity = \Drupal::entityTypeManager()->getStorage($entityType)->load($param); + $param = $params['entity'] ?? $params[$entityType]; + $entity = $this->entityTypeManager->getStorage($entityType)->load($param); } } return $entity; } + } diff --git a/src/Plugin/Block/AluminumCopyrightBlock.php b/src/Plugin/Block/AluminumCopyrightBlock.php index fde8e3f..2522c4c 100644 --- a/src/Plugin/Block/AluminumCopyrightBlock.php +++ b/src/Plugin/Block/AluminumCopyrightBlock.php @@ -11,10 +11,11 @@ * ) */ class AluminumCopyrightBlock extends AluminumBlockBase { + /** * {@inheritdoc} */ - public function getOptions() { + public function getOptions(): array { return [ 'copyright_text' => [ '#type' => 'textfield', @@ -29,9 +30,10 @@ public function getOptions() { /** * {@inheritdoc} */ - public function build() { + public function build(): array { return [ '#markup' => sprintf('

%s

', $this->t($this->getOptionValue('copyright_text', TRUE))), ]; } + } diff --git a/src/Plugin/Block/AluminumDropdownLinksBlock.php b/src/Plugin/Block/AluminumDropdownLinksBlock.php index 158e97a..614604b 100644 --- a/src/Plugin/Block/AluminumDropdownLinksBlock.php +++ b/src/Plugin/Block/AluminumDropdownLinksBlock.php @@ -14,7 +14,7 @@ class AluminumDropdownLinksBlock extends AluminumBlockBase { /** * {@inheritdoc} */ - public function getOptions() { + public function getOptions(): array { return [ 'button_text' => [ '#type' => 'textfield', @@ -32,7 +32,7 @@ public function getOptions() { /** * {@inheritdoc} */ - public function build() { + public function build(): array { return [ '#theme' => 'aluminum_dropdown_links', '#button_text' => $this->getOptionValue('button_text'), @@ -40,7 +40,13 @@ public function build() { ]; } - protected function getList() { + /** + * Get list of dropdown links. + * + * @return array + * An array of dropdown links. + */ + protected function getList(): array { $list = []; $links = $this->getOptionValue('link_list'); @@ -62,4 +68,5 @@ protected function getList() { return $list; } + } diff --git a/src/Plugin/Block/AluminumFollowBlock.php b/src/Plugin/Block/AluminumFollowBlock.php index 620e815..353ff2a 100644 --- a/src/Plugin/Block/AluminumFollowBlock.php +++ b/src/Plugin/Block/AluminumFollowBlock.php @@ -1,12 +1,11 @@ getValue($id . '_icon_class', 'icons'); @@ -110,7 +121,15 @@ protected function iconClass($id) { return $class; } - protected function getSocialNetworks() { + /** + * Get social networks. + * + * @return array + * An array of social networks. + * + * @throws \Drupal\aluminum_storage\Aluminum\Exception\ConfigException + */ + protected function getSocialNetworks(): array { $socialNetworks = aluminum_storage_social_networks(); $networks = []; @@ -140,12 +159,23 @@ protected function getSocialNetworks() { return $networks; } - private function getUrl($id) { + /** + * Get URL. + * + * @param $id + * The id. + * + * @return mixed|null + * The url if found. + * @throws \Drupal\aluminum_storage\Aluminum\Exception\ConfigException + */ + private function getUrl($id): mixed { $config = ConfigManager::getConfig('content'); if ($this->getOptionValue($id . '_url_override')) { $url = $this->getOptionValue($id . '_url'); - } else { + } + else { $url = $config->getValue($id . '_page_url', 'social', ''); } @@ -155,11 +185,12 @@ private function getUrl($id) { /** * {@inheritdoc} */ - public function build() { + public function build(): array { return [ '#theme' => 'aluminum_follow_list', '#list' => $this->getSocialNetworks(), '#link_target' => $this->getOptionValue('link_target', '_blank'), ]; } + } diff --git a/src/Plugin/Block/AluminumFollowCustomBlock.php b/src/Plugin/Block/AluminumFollowCustomBlock.php index 4b9cb35..c943aa1 100644 --- a/src/Plugin/Block/AluminumFollowCustomBlock.php +++ b/src/Plugin/Block/AluminumFollowCustomBlock.php @@ -1,9 +1,6 @@ 'textfield', '#title' => $this->t('Icon'), @@ -48,7 +45,7 @@ public function getOptions() { /** * {@inheritdoc} */ - public function build() { + public function build(): array { $icon = $this->getOptionValue('icon'); if (empty($icon)) { @@ -62,4 +59,5 @@ public function build() { '#link_url' => $this->getOptionValue('link_url'), ]; } + } diff --git a/src/Plugin/Block/AluminumIframeBlock.php b/src/Plugin/Block/AluminumIframeBlock.php index 8d9c233..06cf5c8 100644 --- a/src/Plugin/Block/AluminumIframeBlock.php +++ b/src/Plugin/Block/AluminumIframeBlock.php @@ -2,9 +2,6 @@ namespace Drupal\aluminum_blocks\Plugin\Block; -use Drupal\Core\Annotation\Translation; -use Drupal\Core\Block\Annotation\Block; - /** * Provides an 'Iframe' block * @@ -14,10 +11,11 @@ * ) */ class AluminumIframeBlock extends AluminumBlockBase { + /** * {@inheritdoc} */ - public function getOptions() { + public function getOptions(): array { $options = []; $options['src'] = [ @@ -108,7 +106,13 @@ public function getOptions() { return $options; } - protected function buildWrapperCss() { + /** + * Builds wrapper CSS classes. + * + * @return string + * Wrapper CSS classes. + */ + protected function buildWrapperCss(): string { $center = $this->getOptionValue('center'); $output = ''; @@ -120,13 +124,18 @@ protected function buildWrapperCss() { return $output; } - protected function buildCss() { + /** + * Build CSS. + * + * @return string + * A string of CSS. + */ + protected function buildCss(): string { $css = []; $maxWidth = $this->getOptionValue('max_width'); $maxHeight = $this->getOptionValue('max_height'); - if ($maxWidth > 0) { $css['max-width'] = $maxWidth; } @@ -147,7 +156,7 @@ protected function buildCss() { /** * {@inheritdoc} */ - public function build() { + public function build(): array { return [ '#theme' => 'aluminum_iframe', '#src' => $this->getOptionValue('src'), @@ -163,4 +172,5 @@ public function build() { '#wrapper_css' => $this->buildWrapperCss(), ]; } + } diff --git a/src/Plugin/Block/AluminumLinkBlock.php b/src/Plugin/Block/AluminumLinkBlock.php index 0e30f35..433c884 100644 --- a/src/Plugin/Block/AluminumLinkBlock.php +++ b/src/Plugin/Block/AluminumLinkBlock.php @@ -1,11 +1,17 @@ pathValidator = $path_validator; + $this->aliasManager = $alias_manager; + $this->routeMatch = $route_match; + $this->requestStack = $request_stack; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + /** @var \Drupal\Core\Path\PathValidatorInterface $path_validator */ + $path_validator = $container->get('path.validator'); + /** @var \Drupal\path_alias\AliasManagerInterface $alias_manager */ + $alias_manager = $container->get('path_alias.manager'); + /** @var \Drupal\Core\Routing\RouteMatchInterface $route_match */ + $route_match = $container->get('current_route_match'); + /** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */ + $request_stack = $container->get('request_stack'); + /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */ + $config_factory = $container->get('config.factory'); + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $config_factory, + $path_validator, + $alias_manager, + $route_match, + $request_stack + ); + } + /** * {@inheritdoc} */ - public function getOptions() { + public function getOptions(): array { $options = []; $options['link_text'] = [ @@ -39,7 +128,13 @@ public function getOptions() { return $options; } - protected function isActiveTrail() { + /** + * Is active trail. + * + * @return bool + * True if active trail. + */ + protected function isActiveTrail(): bool { $linkUrl = $this->getOptionValue('link_url'); if (empty($linkUrl) || $linkUrl == '#') { @@ -52,18 +147,18 @@ protected function isActiveTrail() { return FALSE; } - $currentUrl = Url::fromRouteMatch(\Drupal::routeMatch())->getInternalPath(); - - return (strpos($currentUrl, $url->getInternalPath()) === 0); + $currentUrl = Url::fromRouteMatch($this->routeMatch)->getInternalPath(); + return (str_starts_with($currentUrl, $url->getInternalPath())); } /** * {@inheritdoc} */ - public function build() { + public function build(): array { $classes = ['aluminum-link']; - if ($this->getOptionValue('link_url') != '[back]' && $this->isActiveTrail()) { + if ($this->getOptionValue('link_url') != '[back]' + && $this->isActiveTrail()) { $classes[] = 'is-activeTrail'; } @@ -83,29 +178,39 @@ public function build() { return $build; } - protected function getUrl() { + /** + * Get url. + * + * @return array|\Drupal\Core\GeneratedUrl|string|string[] + */ + protected function getUrl(): array|GeneratedUrl|string { $url = $this->getOptionValue('link_url'); - if (strpos($url, '[back]') !== FALSE) { - $previousUrl = \Drupal::request()->server->get('HTTP_REFERER'); - $fake_request = Request::create($previousUrl); - /** @var \Drupal\Core\Url $url_object */ - $url_object = \Drupal::service('path.validator')->getUrlIfValid($fake_request->getRequestUri()); - - if ($url_object) { - $back_url = \Drupal::service('path_alias.manager')->getAliasByPath('/'.$url_object->getInternalPath()); - } else { - $back_url = '/'; + if (str_contains($url, '[back]')) { + $previousUrl = $this->requestStack->getCurrentRequest()->server->get('HTTP_REFERER'); + if ($previousUrl) { + $fake_request = Request::create($previousUrl); + /** @var \Drupal\Core\Url $url_object */ + $url_object = $this->pathValidator->getUrlIfValid($fake_request->getRequestUri()); + + if ($url_object) { + $back_url = $this->aliasManager->getAliasByPath('/' . $url_object->getInternalPath()); + } + else { + $back_url = '/'; + } + $url = str_replace('[back]', $back_url, $url); } - $url = str_replace('[back]', $back_url, $url); } - if (strpos($url, '[current]') !== FALSE) { - $current = \Drupal::request()->getRequestUri(); + if (str_contains($url, '[current]')) { + $current = $this->requestStack->getCurrentRequest()->getRequestUri(); $url = str_replace('[current]', $current, $url); } - if ((strpos($url, '/') === 0) || (strpos($url, '#') === 0) || (strpos($url, '?') === 0)) { + if ((str_starts_with($url, '/')) + || (str_starts_with($url, '#')) + || (str_starts_with($url, '?'))) { $urlObject = Url::fromUserInput($url); $url = $urlObject->toString(); } @@ -113,10 +218,17 @@ protected function getUrl() { return $url; } - public function getCacheContexts() { - //if you depends on \Drupal::routeMatch() - //you must set context of this block with 'route' context tag. - //Every new route this block will rebuild + /** + * Get cache contexts. + * + * @return array|string[] + * An array of cache contexts. + */ + public function getCacheContexts(): array { + // If you depend on \Drupal::routeMatch() + // you must set context of this block with 'route' context tag. + // Every new route this block will rebuild. return Cache::mergeContexts(parent::getCacheContexts(), array('route')); } + } diff --git a/src/Plugin/Block/AluminumLinkListBlock.php b/src/Plugin/Block/AluminumLinkListBlock.php index 2245415..eebfac8 100644 --- a/src/Plugin/Block/AluminumLinkListBlock.php +++ b/src/Plugin/Block/AluminumLinkListBlock.php @@ -11,10 +11,11 @@ * ) */ class AluminumLinkListBlock extends AluminumBlockBase { + /** * {@inheritdoc} */ - public function getOptions() { + public function getOptions(): array { return [ 'link_list' => [ '#type' => 'textarea', @@ -27,14 +28,20 @@ public function getOptions() { /** * {@inheritdoc} */ - public function build() { + public function build(): array { return [ '#theme' => 'aluminum_link_list', '#list' => $this->getList(), ]; } - protected function getList() { + /** + * Gets the link list. + * + * @return array + * A list of links. + */ + protected function getList(): array { $list = []; $links = $this->getOptionValue('link_list'); @@ -56,4 +63,5 @@ protected function getList() { return $list; } + } diff --git a/src/Plugin/Block/AluminumOfficeInfoBlock.php b/src/Plugin/Block/AluminumOfficeInfoBlock.php index 5d76b32..bddbe5b 100644 --- a/src/Plugin/Block/AluminumOfficeInfoBlock.php +++ b/src/Plugin/Block/AluminumOfficeInfoBlock.php @@ -1,15 +1,7 @@ getOptionValue('office_address'); $officeHours = $this->getOptionValue('office_hours'); @@ -90,7 +86,15 @@ public function build() { ]; } - public function getList() { + /** + * Gets list of office info. + * + * @return array + * An array of office info. + * + * @throws \Drupal\aluminum_storage\Aluminum\Exception\ConfigException + */ + public function getList(): array { $list = parent::getList(); $customPhoneNumber = $this->getOptionValue('custom_phone_number'); @@ -105,4 +109,5 @@ public function getList() { return $list; } + } diff --git a/src/Plugin/Block/AluminumPhoneNumberBlock.php b/src/Plugin/Block/AluminumPhoneNumberBlock.php index cf26368..6d99252 100644 --- a/src/Plugin/Block/AluminumPhoneNumberBlock.php +++ b/src/Plugin/Block/AluminumPhoneNumberBlock.php @@ -1,9 +1,8 @@ $info) { @@ -28,7 +34,7 @@ protected function getPhoneNumberOptions() { /** * {@inheritdoc} */ - public function getOptions() { + public function getOptions(): array { return [ 'phone_number_type' => [ '#type' => 'select', @@ -43,7 +49,7 @@ public function getOptions() { /** * {@inheritdoc} */ - public function build() { + public function build(): array { $type = $this->getOptionValue('phone_number_type'); $config = ConfigManager::getConfig('content'); @@ -62,4 +68,5 @@ public function build() { '#url' => $url, ]; } + } diff --git a/src/Plugin/Block/AluminumPhoneNumberListBlock.php b/src/Plugin/Block/AluminumPhoneNumberListBlock.php index f75d4bc..f69e017 100644 --- a/src/Plugin/Block/AluminumPhoneNumberListBlock.php +++ b/src/Plugin/Block/AluminumPhoneNumberListBlock.php @@ -1,15 +1,8 @@ $info) { @@ -33,7 +33,7 @@ protected function getPhoneNumberOptions() { /** * {@inheritdoc} */ - public function getOptions() { + public function getOptions(): array { $options = []; $options['enabled_phone_numbers'] = [ @@ -54,7 +54,15 @@ public function getOptions() { return $options; } - protected function getList() { + /** + * Get list of phone numbers. + * + * @return array + * A list. + * + * @throws \Drupal\aluminum_storage\Aluminum\Exception\ConfigException + */ + protected function getList(): array { $enabled = array_keys(array_filter($this->getOptionValue('enabled_phone_numbers'))); $phone_numbers = aluminum_storage_phone_numbers(); $display_short_titles = $this->getOptionValue('display_short_titles'); @@ -84,10 +92,11 @@ protected function getList() { /** * {@inheritdoc} */ - public function build() { + public function build(): array { return [ '#theme' => 'aluminum_phone_number_list', '#list' => $this->getList(), ]; } + } diff --git a/src/Plugin/Block/AluminumScriptTagBlock.php b/src/Plugin/Block/AluminumScriptTagBlock.php index 308bca5..28c5409 100644 --- a/src/Plugin/Block/AluminumScriptTagBlock.php +++ b/src/Plugin/Block/AluminumScriptTagBlock.php @@ -2,9 +2,6 @@ namespace Drupal\aluminum_blocks\Plugin\Block; -use Drupal\Core\Annotation\Translation; -use Drupal\Core\Block\Annotation\Block; - /** * Provides a 'Script tag' block * @@ -14,10 +11,11 @@ * ) */ class AluminumScriptTagBlock extends AluminumBlockBase { + /** * {@inheritdoc} */ - public function getOptions() { + public function getOptions(): array { $options = []; $options['wrapper_classes'] = [ @@ -47,7 +45,7 @@ public function getOptions() { /** * {@inheritdoc} */ - public function build() { + public function build(): array { $classes = $this->getOptionValue('wrapper_classes'); $classes = trim('AluminumScriptTag ' . $classes); @@ -59,4 +57,5 @@ public function build() { '#script' => $this->getOptionValue('script'), ]; } + } diff --git a/src/Plugin/Block/AluminumSnippetBlock.php b/src/Plugin/Block/AluminumSnippetBlock.php index 5456ce4..8076693 100644 --- a/src/Plugin/Block/AluminumSnippetBlock.php +++ b/src/Plugin/Block/AluminumSnippetBlock.php @@ -2,8 +2,6 @@ namespace Drupal\aluminum_blocks\Plugin\Block; -use Drupal\Core\Annotation\Translation; -use Drupal\Core\Block\Annotation\Block; use Drupal\Core\Render\Markup; /** @@ -18,7 +16,7 @@ class AluminumSnippetBlock extends AluminumBlockBase { /** * {@inheritdoc} */ - public function getOptions() { + public function getOptions(): array { $options = []; $options['wrapper_classes'] = [ @@ -48,7 +46,7 @@ public function getOptions() { /** * {@inheritdoc} */ - public function build() { + public function build(): array { $classes = $this->getOptionValue('wrapper_classes'); $classes = trim('AluminumSnippet ' . $classes); From ecab4ba1aa1681f4f8d0f3c32f1907d9339a00ee Mon Sep 17 00:00:00 2001 From: Scott Sawyer Date: Wed, 23 Jul 2025 17:57:07 -0400 Subject: [PATCH 4/4] Fixes #4 Restructured the conditionals such that [back] always gets replaced. --- src/Plugin/Block/AluminumLinkBlock.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Plugin/Block/AluminumLinkBlock.php b/src/Plugin/Block/AluminumLinkBlock.php index 433c884..f0ac71a 100644 --- a/src/Plugin/Block/AluminumLinkBlock.php +++ b/src/Plugin/Block/AluminumLinkBlock.php @@ -187,7 +187,9 @@ protected function getUrl(): array|GeneratedUrl|string { $url = $this->getOptionValue('link_url'); if (str_contains($url, '[back]')) { + $back_url = '/'; $previousUrl = $this->requestStack->getCurrentRequest()->server->get('HTTP_REFERER'); + if ($previousUrl) { $fake_request = Request::create($previousUrl); /** @var \Drupal\Core\Url $url_object */ @@ -196,11 +198,8 @@ protected function getUrl(): array|GeneratedUrl|string { if ($url_object) { $back_url = $this->aliasManager->getAliasByPath('/' . $url_object->getInternalPath()); } - else { - $back_url = '/'; - } - $url = str_replace('[back]', $back_url, $url); } + $url = str_replace('[back]', $back_url, $url); } if (str_contains($url, '[current]')) {