varbase_bootstrap_paragraphs-9.0.0-alpha1/varbase_bootstrap_paragraphs.module
varbase_bootstrap_paragraphs.module
<?php
/**
* @file
* Varbase Bootstrap Paragraphs module file.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;
use Drupal\Core\Url;
// Include all helpers.
include_once __DIR__ . '/includes/helpers.inc';
/**
* 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)) {
$url = Url::fromUri('internal:/')->toString();
if (substr($url, -1) === '/') {
$url = substr($url, 0, -1);
}
if (\Drupal::moduleHandler()->moduleExists('drimage_improved')) {
// For drimage_improved, construct a proper image URL for background images.
// Background images use the drimage improved route which expects:
// /drimage/{width}/{height}/{fid}/{iwc_id}/{file_extension}
//
// Use 1600px width for responsive background image coverage
// Height 0 = calculate based on aspect ratio
$file_uri = $file->getFileUri();
// Get the file extension to pass to the route
$file_extension = pathinfo($file_uri, PATHINFO_EXTENSION);
// Generate the drimage URL
// Format: /drimage/{width}/{height}/{fid}/{iwc_id}/{file_extension}
$file_path = '/drimage/1600/0/' . $target_id . '/-/' . $file_extension;
}
elseif (\Drupal::moduleHandler()->moduleExists('drimage')) {
// Display Edge-to-Edge auto) fall back default background image using Drimage.
$file_path = $url . '/drimage/1600/0/' . $target_id . '/-' . $url . \Drupal::service('file_url_generator')->generateString($file->getFileUri());
}
}
}
}
$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;
}
}
/**
* 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);
}
/**
* 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;
}
}
// If the field_lp_paragraphs will attached the library.
if (isset($form['field_lp_paragraphs'])) {
$form['#attached']['library'][] = 'varbase_bootstrap_paragraphs/vbp-default-admin';
}
}
