diff --git a/aluminum_blocks.info.yml b/aluminum_blocks.info.yml index 5ea6b6d..8a357ec 100644 --- a/aluminum_blocks.info.yml +++ b/aluminum_blocks.info.yml @@ -1,10 +1,10 @@ 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: ^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' => '
%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 8dfbdaa..f0ac71a 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,38 @@ 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]')) { + $back_url = '/'; + $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()); + } } $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 +217,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);