1 layout.theme.inc template_preprocess_block(&$variables)

Prepares variables for block templates.

File

core/modules/layout/layout.theme.inc, line 390
Theme functions for the Layout module.

Code

function template_preprocess_block(&$variables) {
  $content = $variables['content'];
  $block = $variables['block'];
  $style = $block->style;

  $variables['classes'] = array();
  $variables['attributes'] = array();
  $variables['title_prefix'] = '';
  $variables['title_suffix'] = '';

  // Basic classes.
  $variables['classes'][] = 'block';
  $variables['classes'][] = backdrop_html_class('block-' . $block->module . '-' . (isset($block->childDelta) ? $block->childDelta : $block->delta));

  // Add the hero background image inline. This could also be added in a
  // specific template_preprocess_block__layout__hero() function, but we add
  // it here just to cut down on unnecessary separation.
  if (get_class($block) == 'BlockHero') {
    $variables['classes'][] = 'block-hero';
    if (isset($block->settings['image_path'])) {
      $variables['attributes']['style'] = 'background-image:url(' . $block->settings['image_path'] . ');';
      $variables['classes'][] = 'block-hero-image';
    }
    else {
      $variables['classes'][] = 'block-hero-no-image';
    }
  }

  // Add custom classes if sent in.
  if (!empty($style->settings['classes'])) {
    $new_classes = explode(' ', $style->settings['classes']);
    foreach ($new_classes as $class) {
      $variables['classes'][] = backdrop_clean_css_identifier($class, array());
    }
    $variables['classes'] = array_filter($variables['classes']);
  }

  if (module_exists('contextual') && user_access('access contextual links')) {
    $links = array();
    // These are specified by the content.
    if (!empty($content->admin_links)) {
      $links += $content->admin_links;
    }

    // Take and contextual links that may have been returned in the block render
    // array and move them up into the block wrapper's contextual links.
    if (is_array($content->content) && isset($content->content['#contextual_links'])) {
      $element = array(
        '#type' => 'contextual_links',
        '#contextual_links' => $content->content['#contextual_links'],
        '#element' => $content->content,
      );
      unset($content->content['#contextual_links']);

      $element = contextual_pre_render_links($element);
      $links += $element['#links'];
    }

    if ($links) {
      $build = array(
        '#prefix' => '<div class="contextual-links-wrapper">',
        '#suffix' => '</div>',
        '#theme' => 'links__contextual',
        '#links' => $links,
        '#attributes' => array('class' => array('contextual-links')),
        '#attached' => array(
          'library' => array(array('contextual', 'contextual-links')),
        ),
      );
      $variables['classes'][] = 'contextual-links-region';
      $variables['title_prefix'] = backdrop_render($build);
    }
  }

  // Add a NID class to node blocks.
  if ($block->plugin == 'node:content') {
    $variables['classes'][] = 'block-node-content-' . $block->settings['nid'];
  }

  // Set up some placeholders for constructing template file names.
  $base = 'block';
  $delimiter = '__';

  // Add template file suggestion for content type and sub-type.
  $variables['theme_hook_suggestions'][] = $base . $delimiter . strtr($block->plugin, '-', '_');

  $variables['title'] = !empty($content->title) ? $content->title : '';
  $variables['content'] = !empty($content->content) ? $content->content : '';
}