rocketship_paragraphs-5.0.0-alpha8/rocketship_paragraphs.module
rocketship_paragraphs.module
<?php
/**
* @file
* Contains module functionality.
*/
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\Exception\FileWriteException;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\user\Entity\Role;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Url;
/**
* Implements hook_entity_view_mode_alter().
*
* Change the view mode of USP based on selection options.
*/
function rocketship_paragraphs_entity_view_mode_alter(&$view_mode, EntityInterface $entity) {
if ($entity->bundle() === 'p_007_child') {
/** @var \Drupal\paragraphs\Entity\Paragraph $entity */
$parent = $entity->getParentEntity();
$view_selection = $parent->get('field_p_007_view_mode')->value;
if (empty($view_selection)) {
return;
}
$view_mode = $view_selection;
}
if ($entity->bundle() === 'p_009') {
/** @var \Drupal\paragraphs\Entity\Paragraph $entity */
$view_selection = $entity->get('field_p_009_view_mode')->value;
if (empty($view_selection)) {
return;
}
$view_mode = $view_selection;
}
}
/**
* Implements hook_theme().
*/
function rocketship_paragraphs_theme($existing, $type, $theme, $path) {
// @see https://www.drupal.org/forum/support/module-development-and-code-questions/2015-06-03/drupal-8-override-template-with
$theme_templates = [
// Custom template path for default paragraphs field.
'field__field_paragraphs' => [
'template' => 'rs--field--field-paragraphs',
'base hook' => 'node',
],
// Custom template path for tabbed item field (eg. for FAQ).
'field__field_p_004_item' => [
'template' => 'rs--field--field',
'base hook' => 'node',
],
// Custom template path for p007 USP children field.
'field__field_p_007_children' => [
'template' => 'rs--field--field',
'base hook' => 'node',
],
// Custom template path for p010 child logo bar logos.
'field__field_p_010_children' => [
'template' => 'rs--field--paragraph--p-010-children',
'base hook' => 'node',
],
// Custom template path for p012 child image field.
'field__field_p_012_children' => [
'template' => 'rs--field--paragraph--p-012-children',
'base hook' => 'node',
],
// Custom template path for p014 related items.
'field__field_p_related_items' => [
'template' => 'rs--field--field',
'base hook' => 'node',
],
// Custom template path for load more field.
'field__field_p_load_more' => [
'template' => 'rs--field--field-load-more',
'base hook' => 'node',
],
// Custom template path for multi-image field.
'field__field_p_media_images_unlimited' => [
'template' => 'rs--field--field',
'base hook' => 'node',
],
// Custom template path for button field.
'field__field_p_button' => [
'template' => 'rs--field--field-button',
'base hook' => 'node',
],
// Custom template path for name rule field.
'name_rule_field' => [
'template' => 'rs--name-rule-field',
'variables' => [
'name' => NULL,
'separator' => NULL,
'extra_rule' => NULL,
],
],
];
$theme_templates['paragraph__rocketship_paragraphs'] = [
'template' => 'paragraph--rocketship-paragraphs',
'base hook' => 'paragraph',
];
return $theme_templates;
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for paragraph.
*/
function rocketship_paragraphs_theme_suggestions_paragraph_alter(array &$suggestions, array $variables) {
$entity = $variables['elements']['#paragraph'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$type = $entity->getType();
// These are suggestions that will be picked up in theme and module templates
// folders they need to have a file inside the module templates folder,
// otherwise it will give an error. This is because they are added in
// MODULE_theme().
$suggestions[] = 'paragraph__rocketship_paragraphs';
// These are suggestions that will be picked up only in the theme templates
// folder, if we also want them to be picked up in the module templates, we
// would need to add them to MODULE_theme like we did for
// 'paragraph__rocketship_paragraphs'.
$suggestions[] = 'paragraph__rocketship_paragraphs__' . $type;
$suggestions[] = 'paragraph__rocketship_paragraphs__' . $type . '__' . $sanitized_view_mode;
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function rocketship_paragraphs_paragraphs_type_insert(EntityInterface $entity) {
/** @var \Drupal\paragraphs\ParagraphsTypeInterface $entity */
// Grant view access to everyone
// Grant edit, delete, create access to webadmin and above
// !!! Unless it's overview paragraph type, then only view to webadmin.
$type_id = $entity->id();
// Anon can view all.
$role = Role::load(Role::ANONYMOUS_ID);
if ($role) {
$role->grantPermission("view paragraph content $type_id");
$role->save();
}
// Auth can view all.
$role = Role::load(Role::AUTHENTICATED_ID);
if ($role) {
$role->grantPermission("view paragraph content $type_id");
$role->save();
}
// Webadmin can do everything as long as it isn't overview.
$role = Role::load('webadmin');
if ($role) {
$role->grantPermission("view paragraph content $type_id");
$role->grantPermission("update paragraph content $type_id");
if ($type_id !== 'overview') {
$role->grantPermission("create paragraph content $type_id");
$role->grantPermission("delete paragraph content $type_id");
}
$role->save();
}
}
/**
* Implements hook_entity_field_access().
*/
function rocketship_paragraphs_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
if ($operation != 'view'
&& ($field_definition->getName() == 'field_overview' || $field_definition->getName() == 'field_overview_sidebar_blocks')
&& $field_definition->getTargetEntityTypeId() === 'paragraph'
&& $field_definition->getTargetBundle() === 'overview') {
return AccessResult::forbiddenIf(!$account->hasPermission('administer paragraphs settings'));
}
return AccessResult::neutral();
}
/**
* Implements hook_field_widget_single_element_WIDGET_TYPE_form_alter().
*/
function rocketship_paragraphs_field_widget_single_element_webform_entity_reference_select_form_alter(array &$element, FormStateInterface $form_state, array $context) {
// Hide 'Default webform submission data (YAML)' for non admins.
$user = \Drupal::currentUser();
$element['settings']['default_data']['#access'] = $user->hasPermission('administer webform');
}
/**
* Implements hook_theming_page_attachments().
*/
function rocketship_paragraphs_page_attachments(array &$attachments) {
// Make the form settings available in the javascript, twig, … because we
// need some of them to selectively add HTML classes or JS functions.
$config = \Drupal::config('rocketship_paragraphs.settings');
$cssStructural = $config->get('css_structural');
$cssColors = $config->get('css_colors');
$backgroundsFull = $config->get('backgrounds_full');
$variables['#attached']['drupalSettings']['rocketship_paragraphs']['css_structural'] = $cssStructural;
$variables['#attached']['drupalSettings']['rocketship_paragraphs']['css_colors'] = $cssColors;
$variables['#attached']['drupalSettings']['rocketship_paragraphs']['backgrounds_full'] = $backgroundsFull;
// Check the form settings to see if we need to attach certain libs, we only
// load critical CSS library and colors in front theme, not admin.
if (\Drupal::service('router.admin_context')->isAdminRoute() == FALSE) {
// Only if structural css is chosen.
if ($cssStructural) {
$attachments['#attached']['library'][] = 'rocketship_paragraphs/structural';
}
// Only if colors css is chosen.
if ($cssColors) {
// Only add colors if they have been generated.
$path = 'public://css/style.paragraphs.colors-new.min.css';
if (!is_file($path)) {
_rocketship_paragraphs_generate_background_css_file();
}
$attachments['#attached']['library'][] = 'rocketship_paragraphs/p_colors';
}
// if in admin theme
}
else {
// Add some styling and JS for layout pickers
$attachments['#attached']['library'][] = 'rocketship_paragraphs/p_admin_layouts';
}
}
/**
* Callback function for the field_p_bg_color.
*
* Adds 2 default colors to the list and allows alters for new colors.
*/
function rocketship_paragraphs_background_colors(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL, &$cacheable = TRUE) {
$cacheable = FALSE;
if (!$entity) {
return;
}
$paragraphType = $entity->bundle();
$options = [];
$options['_none'] = t('- None -');
// Config form values for number of color variants set.
$variants = \Drupal::config('rocketship_paragraphs.settings')
->get('color_variants');
foreach ($variants as $idx => $values) {
$name = $values['name'];
$foregroundColor = $values['foreground'];
$backgroundColor = $values['background'];
// Clean up foreground.
$fg = str_replace(
['#', '/', '_', ' '],
['', '', '-', '-',],
$foregroundColor);
$fg = strtolower($fg);
// Clean up background.
$bg = str_replace(
['#', '/', '_', ' '],
['', '', '-', '-',],
$backgroundColor);
$bg = strtolower($bg);
// Add label and value for the bg color.
// Will be made into a class + inline CSS.
$options[$name . '/' . $fg . '/' . $bg] = $name . '/' . $foregroundColor . '/' . $backgroundColor;
}
$theme = \Drupal::configFactory()->get('system.theme')->get('default');
$path = \Drupal::service('extension.list.theme')->getPath($theme) . '/' . "$theme.theme";
if (is_file($path)) {
require_once $path;
}
$function = "{$theme}_rocketship_paragraphs_bg_color_options_list";
if (function_exists($function)) {
$function($options, $paragraphType);
}
return $options;
}
/**
* Implements hook_field_widget_single_element_WIDGET_TYPE_form_alter().
*/
function rocketship_paragraphs_field_widget_single_element_entity_reference_paragraphs_form_alter(array &$element, FormStateInterface $form_state, array $context) {
// If the paragraph type has got a background color field,
// we need to load a library for the admin preview of colors
// (color picker) to work.
if (isset($element['subform']['field_p_bg_color']) && isset($element['subform']['field_p_bg_color']['widget'])) {
// Library for making the colorpicker work.
$element['subform']['#attached']['library'][] = 'rocketship_paragraphs/p_admin_colors';
// let the theme override it if has an override library
$theme = \Drupal::configFactory()->get('system.theme')->get('default');
$library = \Drupal::service('library.discovery')
->getLibraryByName($theme, 'admin_overrides');
if (isset($library)) {
$element['subform']['#attached']['library'][] = $theme . '/admin_overrides';
}
}
}
/**
* Implements hook_field_widget_single_element_WIDGET_TYPE_form_alter().
*/
function rocketship_paragraphs_field_widget_single_element_paragraphs_form_alter(array &$element, FormStateInterface $form_state, array $context) {
rocketship_paragraphs_field_widget_single_element_entity_reference_paragraphs_form_alter($element, $form_state, $context);
}
/**
* Implements hook_field_widget_single_element_WIDGET_TYPE_form_alter().
*/
function rocketship_paragraphs_field_widget_single_element_paragraphs_classic_asymmetric_form_alter(array &$element, FormStateInterface $form_state, array $context) {
rocketship_paragraphs_field_widget_single_element_entity_reference_paragraphs_form_alter($element, $form_state, $context);
}
/**
* Implements hook_field_widget_single_element_WIDGET_TYPE_form_alter().
*/
function rocketship_paragraphs_field_widget_single_element_entity_reference_paragraphs_previewer_form_alter(array &$element, FormStateInterface $form_state, array $context) {
rocketship_paragraphs_field_widget_single_element_entity_reference_paragraphs_form_alter($element, $form_state, $context);
}
/**
* Implements hook_field_widget_single_element_WIDGET_TYPE_form_alter().
*/
function rocketship_paragraphs_field_widget_single_element_paragraphs_previewer_form_alter(array &$element, FormStateInterface $form_state, array $context) {
rocketship_paragraphs_field_widget_single_element_entity_reference_paragraphs_form_alter($element, $form_state, $context);
}
/**
* Implements hook_field_widget_single_element_WIDGET_TYPE_form_alter().
*/
function rocketship_paragraphs_field_widget_single_element_paragraphs_asymmetric_form_alter(array &$element, FormStateInterface $form_state, array $context) {
rocketship_paragraphs_field_widget_single_element_entity_reference_paragraphs_form_alter($element, $form_state, $context);
}
/**
* Implements hook_field_widget_single_element_WIDGET_TYPE_form_alter().
*/
function rocketship_paragraphs_field_widget_single_element_rs_paragraphs_asymmetric_form_alter(array &$element, FormStateInterface $form_state, array $context) {
rocketship_paragraphs_field_widget_single_element_entity_reference_paragraphs_form_alter($element, $form_state, $context);
}
/**
* Implements hook_ENTITY_TYPE_view_alter().
*/
function rocketship_paragraphs_paragraph_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
if (!$entity instanceof ContentEntityInterface) {
return;
}
// Make some form settings available in the javascript, twig, …, because we
// need some of them to selectively add HTML classes or JS functions.
$backgroundsFull = \Drupal::config('rocketship_paragraphs.settings')
->get('backgrounds_full');
if ($backgroundsFull) {
// Set a class for the stretched backgrounds
// We run this check by default, because some paragraphs may have default bg colors
// without using field_p_bg_color or …_image
$build['#attributes']['class'][] = 'has-bg-stretched';
}
// Add JS for all paragraphs.
$build['#attached']['library'][] = 'rocketship_paragraphs/p';
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
// Set background color to all paragraphs that have the common bg color field.
if ($entity->hasfield('field_p_bg_color')) {
$color = $entity->get('field_p_bg_color')->getValue();
if (isset($color[0]['value'])) {
$build['#attributes']['class'][] = 'has-bg';
$split = preg_split("/\//", $color[0]['value'])[0];
$build['#attributes']['class'][] = 'bg--' . $split;
}
}
if ($entity->hasfield('field_p_media_bg_image')) {
/** @var \Drupal\media\MediaInterface $media */
$media = $entity->get('field_p_media_bg_image')->entity;
if ($media) {
$bg_image = $media->get('field_media_image')->getValue();
}
if (!empty($bg_image)) {
$build['#attributes']['class'][] = 'has-bg-image';
}
}
// Set individual paragraphs stuff.
switch ($entity->bundle()) {
// Story paragraph.
case 'p_001':
_rocketship_paragraphs_p_001_view_alter($build, $entity, $display);
break;
// Image paragraph.
case 'p_002':
_rocketship_paragraphs_p_002_view_alter($build, $entity, $display);
break;
// Text Main paragraph.
case 'p_003':
_rocketship_paragraphs_p_003_view_alter($build, $entity, $display);
break;
// FAQ paragraph.
case 'p_004':
_rocketship_paragraphs_p_004_view_alter($build, $entity, $display);
break;
// Testimonial paragraph.
case 'p_005':
_rocketship_paragraphs_p_005_view_alter($build, $entity, $display);
break;
// Video paragraph.
case 'p_006':
_rocketship_paragraphs_p_006_view_alter($build, $entity, $display);
break;
// USP
case 'p_007':
_rocketship_paragraphs_p_007_view_alter($build, $entity, $display);
break;
case 'p_007_child':
_rocketship_paragraphs_p_007_child_view_alter($build, $entity, $display);
break;
// Focus paragraph.
case 'p_008':
_rocketship_paragraphs_p_008_view_alter($build, $entity, $display);
break;
// Photogallery paragraph.
case 'p_009':
_rocketship_paragraphs_p_009_view_alter($build, $entity, $display);
break;
// Logobar paragraph.
case 'p_010':
_rocketship_paragraphs_p_010_view_alter($build, $entity, $display);
break;
// Guidance paragraph.
case 'p_012':
_rocketship_paragraphs_p_012_view_alter($build, $entity, $display);
break;
// Guidance box paragraph.
case 'p_012_child':
_rocketship_paragraphs_p_012_child_view_alter($build, $entity, $display);
break;
// Overview paragraph.
case 'overview':
_rocketship_paragraphs_p_013_view_alter($build, $entity, $display);
break;
case 'p_014':
_rocketship_paragraphs_p_014_view_alter($build, $entity, $display);
break;
// Focus Header.
case 'p_016':
_rocketship_paragraphs_p_016_view_alter($build, $entity, $display);
break;
}
}
/**
* Helper function for the view alter of the story paragraphs.
*
* @param array $build
* The build array.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The paragraph.
* @param Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display.
*/
function _rocketship_paragraphs_p_001_view_alter(
array &$build,
ContentEntityInterface $entity,
EntityViewDisplayInterface $display) {
// Output the layout choice as a class on the wrapper.
$p_001_view_mode = $entity->get('field_p_001_layout')->getValue();
if (isset($p_001_view_mode[0]['value'])) {
$build['#attributes']['class'][] = 'p--view-mode--' . $p_001_view_mode[0]['value'];
$build['#attributes']['class'][] = 'p-001--view-mode--' . $p_001_view_mode[0]['value'];
}
// Check the background color class.
$color = $entity->get('field_p_bg_color')->getValue();
if (!empty($color)) {
$color = $color[0]['value'];
$split = preg_split("/\//", $color)[0];
$build['#attributes']['class'][] = 'p-001--bg--' . $split;
}
// Output the image layout choice as a class on the wrapper.
if ($entity->hasfield('field_p_001_layout_image')) {
// Output the image layout choice as a class on the wrapper.
$p_layout_image = $entity->get('field_p_001_layout_image')->getValue();
if (isset($p_layout_image[0]['value'])) {
$build['#attributes']['class'][] = 'p--layout--' . $p_layout_image[0]['value'];
}
}
}
/**
* Helper function for the view alter of the Image paragraphs.
*
* @param array $build
* The build array.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The paragraph.
* @param Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display.
*/
function _rocketship_paragraphs_p_002_view_alter(
array &$build,
ContentEntityInterface $entity,
EntityViewDisplayInterface $display) {
// Output the image layout choice as a class on the wrapper.
if ($entity->hasfield('field_p_002_layout_image')) {
// Output the image layout choice as a class on the wrapper.
$p_layout_image = $entity->get('field_p_002_layout_image')->getValue();
if (isset($p_layout_image[0]['value'])) {
$build['#attributes']['class'][] = 'p--layout--' . $p_layout_image[0]['value'];
}
}
}
/**
* Helper function for the view alter of the Text Main paragraphs.
*
* @param array $build
* The build array.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The paragraph.
* @param Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display.
*/
function _rocketship_paragraphs_p_003_view_alter(
array &$build,
ContentEntityInterface $entity,
EntityViewDisplayInterface $display) {
// Output the layout choice as a class on the wrapper.
$p_003_view_mode = $entity->get('field_p_003_view_mode')->getValue();
if (isset($p_003_view_mode[0]['value'])) {
$build['#attributes']['class'][] = 'p--view-mode--' . $p_003_view_mode[0]['value'];
$build['#attributes']['class'][] = 'p-003--view-mode--' . $p_003_view_mode[0]['value'];
}
// Check the background color class.
$color = $entity->get('field_p_bg_color')->getValue();
if (!empty($color)) {
$color = $color[0]['value'];
$split = preg_split("/\//", $color)[0];
$build['#attributes']['class'][] = 'p-003--bg--' . $split;
}
}
/**
* Helper function for the view alter of the FAQ paragraphs.
*
* @param array $build
* The build array.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The paragraph.
* @param Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display.
*/
function _rocketship_paragraphs_p_004_view_alter(
array &$build,
ContentEntityInterface $entity,
EntityViewDisplayInterface $display) {
// Attach library.
$build['#attached']['library'][] = 'rocketship_paragraphs/p_004';
// Check the background color class.
$color = $entity->get('field_p_bg_color')->getValue();
if (!empty($color)) {
$color = $color[0]['value'];
$split = preg_split("/\//", $color)[0];
$build['#attributes']['class'][] = 'p-004--bg--' . $split;
}
}
/**
* Helper function for the view alter of the Testimonial paragraphs.
*
* @param array $build
* The build array.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The paragraph.
* @param Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display.
*/
function _rocketship_paragraphs_p_005_view_alter(
array &$build,
ContentEntityInterface $entity,
EntityViewDisplayInterface $display) {
// Set class if there is a avatar image set.
if ($entity->hasfield('field_p_media_image')) {
$image = $entity->get('field_p_media_image')->entity;
if (!empty($build['field_p_media_image']) and !empty($image)) {
$build['#attributes']['class'][] = 'has-image';
}
}
// Background for the paragraph content.
if ($entity->hasfield('field_p_content_bg_color')) {
$color = $entity->get('field_p_content_bg_color')->getValue();
if (isset($color[0]['value'])) {
$split = preg_split("/\//", $color[0]['value'])[0];
$build['#attributes']['class'][] = 'has-content-bg';
$build['#attributes']['class'][] = 'content-bg--' . $split;
$build['#attributes']['class'][] = 'p-005--content-bg--' . $split;
}
}
// Check the background color class.
$color = $entity->get('field_p_bg_color')->getValue();
if (!empty($color)) {
$color = $color[0]['value'];
$split = preg_split("/\//", $color)[0];
$build['#attributes']['class'][] = 'p-005--bg--' . $split;
}
}
/**
* Helper function for the view alter of the Video paragraphs.
*
* @param array $build
* The build array.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The paragraph.
* @param Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display.
*/
function _rocketship_paragraphs_p_006_view_alter(
array &$build,
ContentEntityInterface $entity,
EntityViewDisplayInterface $display) {
// Output the image layout choice as a class on the wrapper.
if ($entity->hasfield('field_video_layout')) {
// Output the image layout choice as a class on the wrapper.
$p_layout_image = $entity->get('field_video_layout')->getValue();
if (isset($p_layout_image[0]['value'])) {
$build['#attributes']['class'][] = 'p--layout--' . $p_layout_image[0]['value'];
}
}
}
/**
* Helper function.
*
* @param array $build
* The build array.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The paragraph.
* @param Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display.
*/
function _rocketship_paragraphs_p_007_view_alter(
array &$build,
ContentEntityInterface $entity,
EntityViewDisplayInterface $display) {
// Check the view mode.
$view_mode = $entity->get('field_p_007_view_mode')->getValue();
if (!empty($view_mode)) {
$view_mode = $view_mode[0]['value'];
// Add class to paragraph attributes.
$build['#attributes']['class'][] = 'p--view-mode--' . str_replace('_', '-', $view_mode);
$build['#attributes']['class'][] = 'p-007--view-mode--' . str_replace('_', '-', $view_mode);
}
// Add class to field attributes as well.
$build['field_p_007_children']['#attributes']['class'][] = 'p__children--view-mode--' . str_replace('_', '-', $view_mode);
$build['field_p_007_children']['#attributes']['class'][] = 'p-007__children--view-mode--' . str_replace('_', '-', $view_mode);
// Check the background color class.
$color = $entity->get('field_p_bg_color')->getValue();
if (!empty($color)) {
$color = $color[0]['value'];
$split = preg_split("/\//", $color)[0];
$build['#attributes']['class'][] = 'p-007--bg--' . $split;
}
}
/**
* Helper function.
*
* @param array $build
* The build array.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The paragraph.
* @param Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display.
*/
function _rocketship_paragraphs_p_007_child_view_alter(
array &$build,
ContentEntityInterface $entity,
EntityViewDisplayInterface $display) {
// TO DO:
// Wrap the content of the title field in a link with that url & title attr
// instead of having to rely on JS (note: don't forget to remove the js in js/p_007.js when done)
// Keep in mind that we probably need to do this in rocketship core -> 'Header text' formatter
// since anything we add to the title field here, will be overridden by the formatter
// Attach library.
$build['#attached']['library'][] = 'rocketship_paragraphs/p_007';
/*
$link = $entity->get('field_p_link')->getValue();
if (!empty($link)) {
// uri doesn't return any internal url's,
// so we convert it using drupal's core url functionality
$linkUri = Url::fromUri($entity->get('field_p_link')->uri);
$linkUli = $linkUri->toString();
// get the link title
$linkTitle = $entity->get('field_p_link')->title;
// add link title and url to the title field...
}
*/
}
/**
* Helper function for the view alter of focus paragraphs.
*
* @param array $build
* The build array.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The paragraph.
* @param Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display.
*/
function _rocketship_paragraphs_p_008_view_alter(
array &$build,
ContentEntityInterface $entity,
EntityViewDisplayInterface $display) {
// Add the slick library.
$build['#attached']['library'][] = 'rocketship_paragraphs/p_008';
// Check the background color class.
$color = $entity->get('field_p_bg_color')->getValue();
if (!empty($color)) {
$color = $color[0]['value'];
$split = preg_split("/\//", $color)[0];
$build['#attributes']['class'][] = 'p-008--bg--' . $split;
}
}
/**
* Helper function for the view alter of photogallery paragraphs.
*
* @param array $build
* The build array.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The paragraph.
* @param Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display.
*/
function _rocketship_paragraphs_p_009_view_alter(
array &$build,
ContentEntityInterface $entity,
EntityViewDisplayInterface $display) {
// Add library.
$build['#attached']['library'][] = 'rocketship_paragraphs/p_009';
// Check the background color class.
$color = $entity->get('field_p_bg_color')->getValue();
if (!empty($color)) {
$color = $color[0]['value'];
$split = preg_split("/\//", $color)[0];
$build['#attributes']['class'][] = 'p-009--bg--' . $split;
}
// 'Load more' option for the images. Field used in the front-end to make a
// fake button + class added if true.
// @todo: get the limit from a field instead of setting it hardcoded to 6
// + set individual classes ('is--hidden') on the field items on load,
// instead of globally hiding all of them with CSS by default
// and don't forget to change the CSS and JS to handle the classes.
if ($entity->hasField('field_p_load_more')) {
$load_more = $entity->get('field_p_load_more')->getValue();
if (!empty($load_more)) {
$load = $load_more[0]['value'];
if ($load) {
// Add class to paragraph attributes, if 'load more' is checked.
$build['#attributes']['class'][] = 'has--load-more has--visible-items';
// Data limit for the JS to use.
$build['#attributes']['data-limit'][] = '6';
}
else {
// Unset the load more in the front-end if false.
unset($build['field_p_load_more']);
}
}
else {
// Unset the load more in the front-end if false.
unset($build['field_p_load_more']);
}
}
}
/**
* Helper function for the view alter of the p_010 logobar paragraphs.
*
* @param array $build
* The build array.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The paragraph.
* @param Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display.
*/
function _rocketship_paragraphs_p_010_view_alter(
array &$build,
ContentEntityInterface $entity,
EntityViewDisplayInterface $display) {
// Add library.
$build['#attached']['library'][] = 'rocketship_paragraphs/p_010';
// Check the autoplay field.
$autoplay = $entity->get('field_p_010_autonav')->getValue();
if (!empty($autoplay)) {
$autoplay = $autoplay[0]['value'];
if ($autoplay) {
$build['field_p_010_children']['#attributes']['class'][] = 'autoplay';
}
}
}
/**
* Helper function for the view alter of the guidance paragraphs.
*
* @param array $build
* The build array.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The paragraph.
* @param Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display.
*/
function _rocketship_paragraphs_p_012_view_alter(
array &$build,
ContentEntityInterface $entity,
EntityViewDisplayInterface $display) {
// Check the view mode.
$view_mode = $entity->get('field_p_012_view_mode')->getValue();
if (!empty($view_mode)) {
$view_mode = $view_mode[0]['value'];
// Add class to paragraph attributes.
$build['#attributes']['class'][] = 'p--view-mode--' . str_replace('_', '-', $view_mode);
$build['#attributes']['class'][] = 'p-012--view-mode--' . str_replace('_', '-', $view_mode);
// Add class to field attributes as well.
$build['field_p_012_children']['#attributes']['class'][] = 'p--view-mode--' . str_replace('_', '-', $view_mode);
$build['field_p_012_children']['#attributes']['class'][] = 'p-012--view-mode--' . str_replace('_', '-', $view_mode);
}
}
/**
* Helper function for the view alter of guidance_box paragraphs.
*
* @param array $build
* The build array.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The paragraph.
* @param Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display.
*/
function _rocketship_paragraphs_p_012_child_view_alter(
array &$build,
ContentEntityInterface $entity,
EntityViewDisplayInterface $display) {
// Add JS to animate the text overlays properly.
$build['#attached']['library'][] = 'rocketship_paragraphs/p_012';
// Check the Guidance mode.
$guidance_mode = $entity->get('field_p_012_child_mode')->getValue();
if (!empty($guidance_mode)) {
$guidance_mode = $guidance_mode[0]['value'];
// Check for existence of an image.
if ($entity->hasfield('field_p_media_image') && !empty($entity->get('field_p_media_image')->entity)) {
$image = $entity->get('field_p_media_image')->entity->get('field_media_image')
->getValue();
// Has an image.
if (!empty($image)) {
// Certain modes don't need image, so unset.
if (($guidance_mode == 'title' || $guidance_mode == 'teaser') && isset($build['field_p_media_image'])) {
unset($build['field_p_media_image']);
// Otherwise, set a class.
}
else {
$build['#attributes']['class'][] = 'has-image';
}
}
}
// Classes for layouts.
$build['#attributes']['class'][] = 'p-012__child--view-mode';
$build['#attributes']['class'][] = 'p__child--view-mode';
$build['#attributes']['class'][] = 'p__child--view-mode--' . str_replace('_', '-', $guidance_mode);
$build['#attributes']['class'][] = 'p-012__child--view-mode--' . str_replace('_', '-', $guidance_mode);
}
// Add the background color class.
$color = $entity->get('field_p_bg_color')->getValue();
if (!empty($color)) {
$color = $color[0]['value'];
$split = preg_split("/\//", $color)[0];
$build['#attributes']['class'][] = 'p-012__child--bg--' . $split;
}
}
/**
* Helper function for the view alter of the p_014 related items.
*
* @param array $build
* The build array.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The paragraph.
* @param Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display.
*/
function _rocketship_paragraphs_p_014_view_alter(
array &$build,
ContentEntityInterface $entity,
EntityViewDisplayInterface $display) {
// Add library.
$build['#attached']['library'][] = 'rocketship_paragraphs/p_014';
}
/**
* Preprocesses the Focus header paragraph.
*
* @param array $build
* The build array.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The paragraph.
* @param Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display.
*/
function _rocketship_paragraphs_p_016_view_alter(
array &$build,
ContentEntityInterface $entity,
EntityViewDisplayInterface $display) {
// Add class to header_image paragraph for text alignment.
$view_mode = $entity->get('field_header_image_text_align')->getValue();
if (!empty($view_mode)) {
$view_mode = $view_mode[0]['value'];
// Add class to paragraph attributes.
$build['#attributes']['class'][] = 'header--view-mode--' . str_replace('_', '-', $view_mode);
}
// Add class to header_image paragraph for text color.
$color = $entity->get('field_header_image_text_color')->getValue();
if (!empty($color)) {
$color = $color[0]['value'];
// Add class to paragraph attributes.
$build['#attributes']['class'][] = 'header--color';
$build['#attributes']['class'][] = 'header--color--' . str_replace('_', '-', $color);
}
}
/**
* Preprocesses the overview paragraph.
*
* @param array $build
* The build array.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The paragraph.
* @param Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display.
*/
function _rocketship_paragraphs_p_013_view_alter(
array &$build,
ContentEntityInterface $entity,
EntityViewDisplayInterface $display) {
// Add class to link.
$link = $entity->get('field_p_link')->getValue();
if (!empty($link)) {
$build['field_p_link']['#attributes']['class'][] = 'more-link';
$build['field_p_link']['#attributes']['class'][] = 'more-link--overview';
}
// Add class to wrapper.
$overview = $entity->get('field_overview')->getValue();
// Added ':'
$filter = [
' ' => '-',
'_' => '-',
'/' => '-',
'[' => '-',
']' => '',
':' => '-',
];
$build['#attributes']['class'][] = 'overview--' . Html::cleanCssIdentifier($overview[0]['value'], $filter);
}
/**
* Generates CSS file.
*
* Find the paragraph CSS file and replace the placeholders (if there are any)
* for the variant name and colors write it to the CSS folder.
*/
function _rocketship_paragraphs_generate_background_css_file() {
$settings = \Drupal::config('rocketship_paragraphs.settings');
$variants = $settings->get('color_variants');
$cssPath = 'public://css';
$cssTemplatePath = \Drupal::service('extension.list.module')->getPath('rocketship_paragraphs') . '/css/style.paragraphs.colors.min.css';
// Make an array of the Changed CSS to save.
$finalCSS = '';
foreach ($variants as $idx => $values) {
// Get the CSS.
$css = file_get_contents($cssTemplatePath);
// Replace the placeholders with our values.
$css = str_replace(
[
'replace_variant_name',
'replace_foreground_color',
'replace_background_color',
'replace_link_color',
'replace_hover_color',
],
[
$values['name'],
$values['foreground'],
$values['background'],
$values['link'],
$values['hover'],
],
$css
);
$finalCSS .= ' ' . $css;
}
// Save new CSS to a file (create if doesn't exist yet)
try {
\Drupal::service('file_system')
->prepareDirectory($cssPath, FileSystemInterface::CREATE_DIRECTORY);
\Drupal::service('file_system')
->saveData($finalCSS, $cssPath . '/style.paragraphs.colors-new.min.css', FileSystemInterface::EXISTS_REPLACE);
}
catch (FileWriteException $e) {
\Drupal::messenger()->addError(t('The file could not be created.'));
}
catch (\Exception $e) {
\Drupal::messenger()->addError('Error saving file: ' . $e->getMessage());
}
/*
* File is then attached in a library, via hook_page_attachments.
* Other ways of adding it, would be:
*
* can modify the library via HOOK_library_info_alter(&$libraries, $extension)
* https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21theme.api.php/function/hook_library_info_alter/8.2.x
*
* OR we can inject the CSS inline in the head directly
* @see https://www.alloymagnetic.com/blog/64/drupal-8-quick-tip-attaching-inline-css
* example:
* $variables['page']['#attached']['html_head'][] = [[
* '#tag' => 'style',
* '#value' => $mycss_as_a_string,
* ], 'paragraphs-css'];
*
*/
}
