Skip to content

Instantly share code, notes, and snippets.

@johnny5th
Last active April 4, 2018 15:09
Show Gist options
  • Save johnny5th/ce63b74dcdd70ef5b9ff6629dc58171e to your computer and use it in GitHub Desktop.
Save johnny5th/ce63b74dcdd70ef5b9ff6629dc58171e to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\my_module\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'HomeHeader' block.
*
* @Block(
* id = "home_header",
* admin_label = @Translation("Home Header"),
* context = {
* "node" = @ContextDefinition("entity:node", label = @Translation("Node"))
* }
* )
*/
class HomeHeader extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$node = $this->getContextValue('node');
$build = [];
$build['title'] = [
'#title' => $node->title->value,
'#theme' => 'page_title',
];
if ($node->hasField('field_hero_description') && !$node->get('field_hero_description')->isEmpty()) {
$build['description'] = $node->field_hero_description->view('teaser');
}
if ($node->hasField('field_hero_link') && !$node->get('field_hero_link')->isEmpty()) {
$build['link'] = $node->field_hero_link->view('teaser');
}
return $build;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment