You are here

function block_titlelink_preprocess_block in Block Title Link 8

Same name and namespace in other branches
  1. 6.2 block_titlelink.module \block_titlelink_preprocess_block()
  2. 6 block_titlelink.module \block_titlelink_preprocess_block()
  3. 7 block_titlelink.module \block_titlelink_preprocess_block()

Implements hook_preprocess_hook().

File

./block_titlelink.module, line 113
module for adding a link to a block title

Code

function block_titlelink_preprocess_block(&$vars) {

  // Get the block id
  $block_id = $vars['elements']['#id'];
  $config = \Drupal::config('block_titlelink.settings');
  $url = $config
    ->get($block_id . '.title_link');
  $url = isset($url) ? trim($url) : NULL;
  $display = $config
    ->get($block_id . '.display_link');
  $display = isset($display) ? $display : TRUE;
  $titlelink_title = $config
    ->get($block_id . '.title');
  $titlelink_title = isset($titlelink_title) ? Html::escape($titlelink_title) : NULL;
  $target = $config
    ->get($block_id . '.target');
  $target = isset($target) ? $target : NULL;
  if (\Drupal::moduleHandler()
    ->moduleExists('php')) {
    $url = php_eval($url);
  }
  if (!empty($url) && $display) {
    $attributes = [
      'attributes' => [
        'class' => [
          'block-title-link',
        ],
      ],
      'html' => TRUE,
    ];
    if (!empty($titlelink_title)) {
      $attributes['attributes']['title'] = $titlelink_title;
    }
    if (!empty($target)) {
      $attributes['attributes']['target'] = $target;
    }
    $attributes = new Attribute($attributes['attributes']);
    $vars['label'] = Xss::filter('<a href="' . $url . '"' . $attributes . '>' . t($vars['label']) . '</a>');
  }
}