Skip to content

Instantly share code, notes, and snippets.

@langelhc
Created June 7, 2022 17:08
Show Gist options
  • Save langelhc/4e8b89ec2745fd9619342b41730de454 to your computer and use it in GitHub Desktop.
Save langelhc/4e8b89ec2745fd9619342b41730de454 to your computer and use it in GitHub Desktop.
outline preprocess block
<?php
namespace Drupal\outline_design_system\Plugin\Preprocess\Block;
/**
* Highlight CTA block preprocess.
*
* @Preprocess(
* id = "outline_design_system.preprocess.block.highlight_cta",
* hook = "block__highlight_cta"
* )
*/
class BlockHighlightCta extends Block {
/**
* {@inheritdoc}
*/
public function preprocess(array $variables): array {
$variables = parent::preprocess($variables);
$content = $variables['content'];
$variables['description'] = $content['field_highlight_cta_body'];
/** @var \Drupal\block_content\Entity\BlockContent */
$block = $variables['content']['#block_content'];
$variables['background_color'] = $block->field_highlight_cta_bkgrnd->value;
$variables['image_align'] = $block->field_highlight_cta_image_align->value;
$variables['url'] = $block->field_highlight_cta_cta->uri;
$variables['cta'] = $block->field_highlight_cta_cta->title;
$variables['title'] = $block->field_highlight_cta_heading->value;
$alt = $block->field_highlight_cta_image->entity->get('field_media_image')->alt;
$variables['alt'] = $alt;
$uri = $block->field_highlight_cta_image->entity->get('field_media_image')->entity->uri->value;
$variables['src'] = \Drupal::service('file_url_generator')->generateAbsoluteString($uri);
return $variables;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment