function varbase_bootstrap_paragraphs_preprocess_paragraph in Varbase Bootstrap Paragraphs 9.0.x
Same name and namespace in other branches
- 8.7 varbase_bootstrap_paragraphs.module \varbase_bootstrap_paragraphs_preprocess_paragraph()
- 8.4 varbase_bootstrap_paragraphs.module \varbase_bootstrap_paragraphs_preprocess_paragraph()
- 8.5 varbase_bootstrap_paragraphs.module \varbase_bootstrap_paragraphs_preprocess_paragraph()
- 8.6 varbase_bootstrap_paragraphs.module \varbase_bootstrap_paragraphs_preprocess_paragraph()
Implements hook_preprocess_paragraph().
File
- ./
varbase_bootstrap_paragraphs.module, line 34 - Varbase Bootstrap Paragraphs module file.
Code
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,
],
]);
}