You are here

function varbase_bootstrap_paragraphs_preprocess_paragraph in Varbase Bootstrap Paragraphs 8.6

Same name and namespace in other branches
  1. 8.7 varbase_bootstrap_paragraphs.module \varbase_bootstrap_paragraphs_preprocess_paragraph()
  2. 8.4 varbase_bootstrap_paragraphs.module \varbase_bootstrap_paragraphs_preprocess_paragraph()
  3. 8.5 varbase_bootstrap_paragraphs.module \varbase_bootstrap_paragraphs_preprocess_paragraph()
  4. 9.0.x varbase_bootstrap_paragraphs.module \varbase_bootstrap_paragraphs_preprocess_paragraph()

Implements hook_preprocess_paragraph().

File

./varbase_bootstrap_paragraphs.module, line 36
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 col-md-offset-4 col-sm-8 col-sm-offset-2',
    'paragraph--width--narrow' => 'col-md-6 col-md-offset-3 col-sm-10 col-sm-offset-1',
    'paragraph--width--medium' => 'col-md-8 col-md-offset-2',
    'paragraph--width--wide' => 'col-md-10 col-md-offset-1',
    'paragraph--width--full' => 'col-xs-12',
    'bg-edge2edge' => 'bg-edge2edge col-xs-12 no-padding',
  ];
  $width = 'col-xs-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,
    ],
  ]);
}