ws_event-1.0.16/src/Plugin/Block/EventImage.php
src/Plugin/Block/EventImage.php
<?php
namespace Drupal\ws_event\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides an Event Image block.
*
* @Block(
* id = "lb_event_image",
* admin_label = @Translation("Event Image"),
* category = @Translation("Event blocks")
* )
*/
class EventImage extends BlockBase implements ContainerFactoryPluginInterface {
/**
* The route provider.
*
* @var \Drupal\Core\Routing\CurrentRouteMatch
*/
protected $currentRouteMatch;
/**
* Cuurent node.
*
* @var \Drupal\node\Entity\Node
*/
protected $node;
/**
* Constructs a new EventImageBlock instance.
*
* @param array $configuration
* The plugin configuration, i.e. an array with configuration values keyed
* by configuration option name. The special key 'context' may be used to
* initialize the defined contexts by setting it to an array of context
* values keyed by context names.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Routing\CurrentRouteMatch $currentRouteMatch
* The current route.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, CurrentRouteMatch $currentRouteMatch) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->currentRouteMatch = $currentRouteMatch;
$this->node = $currentRouteMatch->getParameter('node');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('current_route_match')
);
}
/**
* {@inheritdoc}
*/
public function build() {
if (!$this->node || !$this->node->hasField('field_header_image')) {
return [];
}
$image = $this->node->get('field_header_image')->view([
'label' => 'hidden',
'type' => 'entity_reference_entity_view',
'settings' => [
'view_mode' => 'event_image',
'link' => FALSE,
],
]);
$build['image'] = $image;
return $build;
}
}
