field_css-2.0.0-rc4/field_css.module
field_css.module
<?php
/**
* @file
* Primary module hooks for Field CSS module.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\field_css\Traits\CssTrait;
/**
* Implements hook_entity_view_alter().
*/
function field_css_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
$prefixes = (
new class() {
use CssTrait;
}
)->itemPrefixes($entity, $display->getMode());
if ($prefixes) {
foreach ($prefixes as $prefix) {
$build['#attributes']['class'][] = $prefix;
// Layout Builder strips attributes before rendering entities as
// components. As a workaround, the classes are added to a custom
// #field_css array, which can be passed through to Layout Builder.
// Using a #field_css guarantees only classes added here are added back
// in \Drupal\field_css\EventSubscriber\BlockComponentRenderArray.
$build['#field_css']['class'][] = $prefix;
}
}
}
/**
* Implements hook_entity_field_access().
*/
function field_css_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
if ($operation == 'edit' && $field_definition->getType() == 'css') {
return AccessResult::forbiddenIf(!$account->hasPermission('access css fields'))
->addCacheContexts(['user.permissions']);
}
return AccessResult::neutral();
}
/**
* Implements hook_codemirror_editor_assets_alter().
*/
function css_field_codemirror_editor_assets_alter(array &$assets) {
// Ensure CodeMirror provides CSS mode.
$assets['js'][] = 'mode/css/css.min.js';
}
