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
2 changes: 1 addition & 1 deletion .github/workflows/addtoprojects.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ jobs:
# You can target a repository in a different organization
# to the issue
project-url: https://github.com/orgs/emulsify-ds/projects/6
github-token: ${{ secrets.ADD_TO_PROJECT_PAT }}
github-token: ${{ secrets.GH_TOKEN }}
29 changes: 29 additions & 0 deletions config/schema/emulsify_tools.schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
emulsify_tools.favicon_package.*:
type: config_entity
label: 'Favicon Package config'
mapping:
id:
type: string
label: 'ID'
label:
type: label
label: 'Label'
uuid:
type: string
tags:
type: sequence
label: 'Tags'
archive:
type: sequence
label: 'Archive'

emulsify_tools.settings:
type: config_object
label: 'Emulsify Tools settings'
mapping:
themes:
type: sequence
label: 'Theme Favicon Mappings'
sequence:
type: string
label: 'Favicon Package ID'
3 changes: 3 additions & 0 deletions emulsify_tools.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ description: Toolset of useful Twig extensions and a subtheme generation command
core_version_requirement: ^10 || ^11
package: Emulsify

dependencies:
- drupal:file

# Information added by Drupal.org packaging script on 2023-02-16
version: '1.0.2'
project: 'emulsify_tools'
Expand Down
132 changes: 132 additions & 0 deletions emulsify_tools.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

/**
* @file
* Contains emulsify_tools.module.
*/

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;

/**
* Implements hook_form_system_theme_settings_alter().
*/
function emulsify_tools_form_system_theme_settings_alter(&$form, FormStateInterface $form_state, $form_id = NULL) {
// Only add this to the global appearance settings.
$build_info = $form_state->getBuildInfo();
$theme = $build_info['args'][0] ?? '';
if (!empty($theme)) {
return;
}

$form['favicon_packages_wrapper'] = [
'#type' => 'details',
'#title' => t('Favicon Packages'),
'#open' => TRUE,
'#weight' => -10,
];

$add_url = Url::fromRoute('entity.favicon_package.add_form', [], ['query' => ['destination' => '/admin/appearance/settings']]);
$form['favicon_packages_wrapper']['add_link'] = [
'#type' => 'link',
'#title' => t('Add new Favicon Package'),
'#url' => $add_url,
'#attributes' => ['class' => ['button', 'button--primary', 'button--small']],
];

// List existing packages.
$list_builder = \Drupal::entityTypeManager()->getListBuilder('favicon_package');
$form['favicon_packages_wrapper']['package_list'] = $list_builder->render();

// Mapping theme to packages.
$config = \Drupal::config('emulsify_tools.settings');
$config_themes = $config->get('themes') ?: [];

$form['favicon_packages_wrapper']['theme_mappings'] = [
'#type' => 'details',
'#title' => t('Theme Favicon Mappings'),
'#description' => t('Assign a favicon package to each theme.'),
'#open' => TRUE,
'#tree' => TRUE,
];

$packages = \Drupal::entityTypeManager()->getStorage('favicon_package')->loadMultiple();
$favicon_options = [0 => t('- Use Drupal Default -')];
foreach ($packages as $package) {
$favicon_options[$package->id()] = $package->label();
}

$themes = \Drupal::service('theme_handler')->listInfo();
uasort($themes, 'Drupal\Core\Extension\ExtensionList::sortByName');

foreach ($themes as $id => $theme) {
if (!empty($theme->info['hidden'])) {
continue;
}
if (!empty($theme->status)) {
$form['favicon_packages_wrapper']['theme_mappings'][$id] = [
'#type' => 'select',
'#title' => t('@name Favicon', ['@name' => $theme->info['name']]),
'#options' => $favicon_options,
'#default_value' => isset($config_themes[$id]) ? $config_themes[$id] : 0,
];
}
}

$form['#submit'][] = 'emulsify_tools_system_theme_settings_submit';
}

/**
* Submit handler for system_theme_settings.
*/
function emulsify_tools_system_theme_settings_submit($form, FormStateInterface $form_state) {
$mappings = $form_state->getValue('theme_mappings');
if ($mappings) {
\Drupal::configFactory()->getEditable('emulsify_tools.settings')
->set('themes', array_filter($mappings))
->save();
}
}

/**
* Implements hook_page_attachments_alter().
*/
function emulsify_tools_page_attachments_alter(array &$attachments) {
$theme = \Drupal::theme()->getActiveTheme()->getName();
/** @var \Drupal\emulsify_tools\FaviconManagerInterface $faviconManager */
$faviconManager = \Drupal::service('emulsify_tools.favicon.manager');

if ($tags = $faviconManager->getTags($theme)) {
// Remove default favicon from html_head_link.
if (!empty($attachments['#attached']['html_head_link'])) {
foreach ($attachments['#attached']['html_head_link'] as $i => $item) {
if (!empty($item) && is_array($item)) {
foreach ($item as $ii => $iitem) {
if (isset($iitem['rel']) && in_array($iitem['rel'], ['shortcut icon', 'icon'])) {
unset($attachments['#attached']['html_head_link'][$i][$ii]);
}
}
if (empty($attachments['#attached']['html_head_link'][$i])) {
unset($attachments['#attached']['html_head_link'][$i]);
}
}
}
if (empty($attachments['#attached']['html_head_link'])) {
unset($attachments['#attached']['html_head_link']);
}
}
// Attach favicon tags.
$attachments['#attached']['html_head'][] = [
[
'#type' => 'markup',
'#markup' => $tags,
'#allowed_tags' => ['link', 'meta'],
'#cache' => [
'tags' => $faviconManager->getCacheTags(),
],
],
'emulsify_tools_favicon',
];
}
}
31 changes: 31 additions & 0 deletions emulsify_tools.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
entity.favicon_package.collection:
path: '/admin/structure/favicon-package'
defaults:
_entity_list: 'favicon_package'
_title: 'Favicon Packages'
requirements:
_permission: 'administer site configuration'

entity.favicon_package.add_form:
path: '/admin/structure/favicon-package/add'
defaults:
_entity_form: 'favicon_package.add'
_title: 'Add Favicon Package'
requirements:
_permission: 'administer site configuration'

entity.favicon_package.edit_form:
path: '/admin/structure/favicon-package/{favicon_package}/edit'
defaults:
_entity_form: 'favicon_package.edit'
_title: 'Edit Favicon Package'
requirements:
_permission: 'administer site configuration'

entity.favicon_package.delete_form:
path: '/admin/structure/favicon-package/{favicon_package}/delete'
defaults:
_entity_form: 'favicon_package.delete'
_title: 'Delete Favicon Package'
requirements:
_permission: 'administer site configuration'
3 changes: 3 additions & 0 deletions emulsify_tools.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ services:
- { name: drush.command }
emulsify_tools.subtheme_generator:
class: Drupal\emulsify_tools\SubThemeGenerator
emulsify_tools.favicon.manager:
class: Drupal\emulsify_tools\FaviconManager
arguments: ['@entity_type.manager', '@config.factory', '@cache.data']
5 changes: 0 additions & 5 deletions src/BemTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Drupal\Core\Template\Attribute;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use Drupal\Component\Utility\Html;

/**
* Class DefaultService.
Expand Down Expand Up @@ -130,10 +129,6 @@ public function bem($context, $base_class, $modifiers = [], $blockname = '', $ex
}
// Add class attribute.
if (!empty($classes)) {
// Escape the css classes added to prevent security issues.
$classes = array_map(function($css_class) {
return Html::cleanCssIdentifier($css_class);
}, $classes);
$attributes->setAttribute('class', $classes);
}
return $attributes;
Expand Down
Loading