Last active
April 4, 2018 15:09
-
-
Save johnny5th/ce63b74dcdd70ef5b9ff6629dc58171e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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