Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions aluminum_blocks.info.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions aluminum_blocks.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}
49 changes: 35 additions & 14 deletions aluminum_blocks.module
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand All @@ -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' => [
Expand All @@ -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) {

}
130 changes: 114 additions & 16 deletions src/Plugin/Block/AluminumAccountLinkBlock.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php

namespace Drupal\aluminum_blocks\Plugin\Block;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Block\Annotation\Block;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides an 'Account link' block
Expand All @@ -15,10 +19,67 @@
* )
*/
class AluminumAccountLinkBlock extends AluminumBlockBase {

/**
* AccountProxyInterface definition.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $account;

/**
* RouteMatchInterface definition.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;

/**
* 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\Session\AccountProxyInterface $account
* The account proxy.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, AccountProxyInterface $account, RouteMatchInterface $route_match) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $config_factory);
$this->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'] = [
Expand All @@ -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(),
Expand All @@ -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'));
}

}
12 changes: 10 additions & 2 deletions src/Plugin/Block/AluminumAddThisBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
* )
*/
class AluminumAddThisBlock extends AluminumBlockBase {

/**
* {@inheritdoc}
*/
public function build() {
public function build(): array {
$build = [
'#markup' => '<div class="AddThis"><div class="addthis_inline_share_toolbox"></div></div>',
'#attached' => ['library' => ['aluminum_blocks/aluminum_addthis']],
Expand All @@ -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'));
}

}
Loading