varbase_bootstrap_paragraphs.module in Varbase Bootstrap Paragraphs 9.0.x
Same filename and directory in other branches
Varbase Bootstrap Paragraphs module file.
File
varbase_bootstrap_paragraphs.moduleView source
<?php
/**
* @file
* Varbase Bootstrap Paragraphs module file.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
/**
* Implements hook_theme().
*/
function varbase_bootstrap_paragraphs_theme($existing, $type, $theme, $path) {
return [
'paragraph__default' => [
'base hook' => 'paragraph',
],
'paragraph__bp_accordion' => [
'base hook' => 'paragraph',
],
'paragraph__bp_carousel' => [
'base hook' => 'paragraph',
],
'paragraph__bp_tabs' => [
'base hook' => 'paragraph',
],
'paragraph__bp_columns_three_uneven' => [
'base hook' => 'paragraph',
],
'paragraph__bp_columns_two_uneven' => [
'base hook' => 'paragraph',
],
'paragraph__bp_image' => [
'base hook' => 'paragraph',
],
'paragraph__bp_modal' => [
'base hook' => 'paragraph',
],
'field__entity_reference_revisions' => [
'base hook' => 'paragraph',
],
'field__paragraph__bp_column_content' => [
'base hook' => 'paragraph',
],
'field__paragraph__bp_image__image' => [
'base hook' => 'paragraph',
],
];
}
/**
* Implements hook_preprocess_paragraph().
*/
function varbase_bootstrap_paragraphs_preprocess_paragraph(&$variables) {
$file_path = NULL;
if (isset($variables['paragraph']->bp_image_field) && isset($variables['paragraph']->bp_image_field->target_id)) {
// Get the target id and build the url.
$paragraph =& $variables['paragraph'];
if (isset($paragraph
->get('bp_image_field')->entity) && isset($paragraph
->get('bp_image_field')->entity->field_media_image) && isset($paragraph
->get('bp_image_field')->entity->field_media_image->target_id)) {
$target_id = $paragraph
->get('bp_image_field')->entity->field_media_image->target_id;
$file = File::load($target_id);
if (isset($file)) {
$file_uri = $file
->getFileUri();
$style = ImageStyle::load('de2e');
$file_path = $style
->buildUrl($file_uri);
}
}
}
$width_value = NULL;
if (isset($variables['paragraph']->bp_width) && isset($variables['paragraph']->bp_width->value)) {
$width_value = $variables['paragraph']->bp_width->value;
}
$width_map = [
'paragraph--width--tiny' => 'col-md-4 offset-md-4 col-sm-8 offset-sm-2',
'paragraph--width--narrow' => 'col-md-6 offset-md-3 col-sm-10 offset-sm-1',
'paragraph--width--medium' => 'col-md-8 offset-md-2',
'paragraph--width--wide' => 'col-md-10 offset-md-1',
'paragraph--width--full' => 'col-12',
'bg-edge2edge' => 'bg-edge2edge col-12 p-0',
];
$width = 'col-12';
if (isset($width_map[$width_value])) {
$width = $width_map[$width_value];
}
$custom_paragraph_classes_value = NULL;
if (isset($variables['paragraph']->bp_classes) && isset($variables['paragraph']->bp_classes->value)) {
$custom_paragraph_classes_value = $variables['paragraph']->bp_classes->value;
}
$gutter_value = FALSE;
if (isset($variables['paragraph']->bp_gutter) && isset($variables['paragraph']->bp_gutter->value)) {
$gutter_value = $variables['paragraph']->bp_gutter->value;
}
$title_status = FALSE;
if (isset($variables['paragraph']->bp_title_status) && isset($variables['paragraph']->bp_title_status->value)) {
$title_status = $variables['paragraph']->bp_title_status->value;
}
// Add variables to template.
_varbase_bootstrap_paragraphs_add_template_variable($variables, [
'background_image' => [
'type' => 'image',
'url' => $file_path,
],
'bp_width' => [
'type' => 'string',
'value' => $width,
],
'bp_classes' => [
'type' => 'string',
'value' => $custom_paragraph_classes_value,
],
'bp_gutter' => [
'type' => 'bool',
'value' => $gutter_value,
],
'bp_title_status' => [
'type' => 'bool',
'value' => $title_status,
],
]);
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*
* Widget : Paragraphs Classic : entity_reference_paragraphs.
*/
function varbase_bootstrap_paragraphs_field_widget_entity_reference_paragraphs_form_alter(&$element, &$form_state, $context) {
// Add the Varbase Bootstrap Paragraphs default admin styling.
$element['subform']['#attached']['library'][] = 'varbase_bootstrap_paragraphs/vbp-default-admin';
// If the paragraph type has got a background color field.
if (isset($element['subform']['bp_background']) && isset($element['subform']['bp_background']['widget'])) {
$configFactory = \Drupal::configFactory()
->getEditable('varbase_bootstrap_paragraphs.settings');
$background_colors = $configFactory
->get('background_colors');
$background_colors_options = [
'_none' => t('N/A'),
];
$lines = explode(PHP_EOL, $background_colors);
foreach ($lines as $line) {
$line = explode('|', $line);
$background_colors_options[$line[0]] = $line[1];
}
// Updated the bp_background options with the list of vbp colors.
$element['subform']['bp_background']['widget']['#options'] = $background_colors_options;
// Get the default active theme for the site.
$default_system_theme = \Drupal::configFactory()
->getEditable('system.theme');
$default_active_theme_name = $default_system_theme
->get('default');
$default_active_theme_libraries = \Drupal::service('library.discovery')
->getLibrariesByExtension($default_active_theme_name);
// If the default active theme has got the vbp-colors library use it.
if (isset($default_active_theme_libraries['vbp-colors'])) {
$element['subform']['#attached']['library'][] = $default_active_theme_name . '/vbp-colors';
}
else {
$element['subform']['#attached']['library'][] = 'varbase_bootstrap_paragraphs/vbp-colors';
}
}
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*
* Widget type : Paragraphs EXPERIMENTAL : paragraphs .
*/
function varbase_bootstrap_paragraphs_field_widget_paragraphs_form_alter(&$element, &$form_state, $context) {
varbase_bootstrap_paragraphs_field_widget_entity_reference_paragraphs_form_alter($element, $form_state, $context);
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*
* Widget : Paragraphs Classic Asymmetric : paragraphs_classic_asymmetric .
*/
function varbase_bootstrap_paragraphs_field_widget_paragraphs_classic_asymmetric_form_alter(&$element, &$form_state, $context) {
varbase_bootstrap_paragraphs_field_widget_entity_reference_paragraphs_form_alter($element, $form_state, $context);
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*
* Widget : Paragraphs Previewer & Paragraphs Classic :
* entity_reference_paragraphs_previewer .
*/
function varbase_bootstrap_paragraphs_field_widget_entity_reference_paragraphs_previewer_form_alter(&$element, &$form_state, $context) {
varbase_bootstrap_paragraphs_field_widget_entity_reference_paragraphs_form_alter($element, $form_state, $context);
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*
* Widget : Paragraphs Previewer & Paragraphs EXPERIMENTAL :
* paragraphs_previewer.
*/
function varbase_bootstrap_paragraphs_field_widget_paragraphs_previewer_form_alter(&$element, &$form_state, $context) {
varbase_bootstrap_paragraphs_field_widget_entity_reference_paragraphs_form_alter($element, $form_state, $context);
}
/**
* Populates TWIG variables with VBP related data.
*
* E.g.: $variables['VBP']['test'] becomes
* {{ VBP.test }} in the templates.
*
* @param array &$variables
* The core $variables passed by reference.
* @param array $data
* New data in array format, which will be passed to the template.
*
* @return bool|array
* The new data.
*
* @internal
*/
function _varbase_bootstrap_paragraphs_add_template_variable(array &$variables, array $data = NULL) {
static $key = 'VBP';
if (!isset($variables[$key])) {
$variables[$key] = $data;
}
else {
$variables[$key][] = $data;
}
return $data;
}
/**
* Implements hook_form_alter().
*/
function varbase_bootstrap_paragraphs_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Only for paragraph entity edit forms.
if (isset($form['#entity_type']) && $form['#entity_type'] == 'paragraph' && preg_match("/paragraph_(.*)_entity_edit_form\$/", $form_id)) {
// If the paragraph type has got a background color field.
if (isset($form['bp_background']) && isset($form['bp_background']['widget'])) {
// Add the Varbase Bootstrap Paragraphs default admin styling.
$form['#attached']['library'][] = 'varbase_bootstrap_paragraphs/vbp-default-admin';
$configFactory = \Drupal::configFactory()
->getEditable('varbase_bootstrap_paragraphs.settings');
$background_colors = $configFactory
->get('background_colors');
$background_colors_options = [
'_none' => t('N/A'),
];
$lines = explode(PHP_EOL, $background_colors);
foreach ($lines as $line) {
$line = explode('|', $line);
$background_colors_options[$line[0]] = $line[1];
}
// Updated the bp_background options with the list of vbp colors.
$form['bp_background']['widget']['#options'] = $background_colors_options;
// Get the default active theme for the site.
$default_system_theme = \Drupal::configFactory()
->getEditable('system.theme');
$default_active_theme_name = $default_system_theme
->get('default');
$default_active_theme_libraries = \Drupal::service('library.discovery')
->getLibrariesByExtension($default_active_theme_name);
// If the default active theme has got the vbp-colors library use it.
if (isset($default_active_theme_libraries['vbp-colors'])) {
$form['#attached']['library'][] = $default_active_theme_name . '/vbp-colors';
}
else {
$form['#attached']['library'][] = 'varbase_bootstrap_paragraphs/vbp-colors';
}
}
}
}