drupalorg-1.0.x-dev/src/Plugin/Block/DrupalOrgSponsorWidget.php
src/Plugin/Block/DrupalOrgSponsorWidget.php
<?php
namespace Drupal\drupalorg\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\NodeInterface;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
/**
* Provides a 'DrupalOrg Sponsor Widget' Block.
*
* @Block(
* id = "drupalorg_sponsor_widget",
* admin_label = @Translation("DrupalOrg Sponsor Widget"),
* )
*/
class DrupalOrgSponsorWidget extends BlockBase implements ContainerFactoryPluginInterface {
/**
* The current route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* Constructs a new DrupalOrgSponsorWidget instance.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->routeMatch = $route_match;
}
/**
* {@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() {
/** @var \Drupal\node\NodeInterface $node */
$node = $this->routeMatch->getParameter('node');
if (!$node) {
return [];
}
if (!$node->hasField('field_sponsor')) {
return [];
}
if (!$node->field_sponsor->isEmpty()) {
$sponsor_data = $this->getSponsorData($node);
}
else {
$sponsor_data = $this->initSponsorData();
$sponsor_info_found = FALSE;
if ($node->hasField('og_group_ref_documentation') && !$node->og_group_ref_documentation->isEmpty()) {
// Find the nearest parent with field_sponsor information, and then
// check the type.
$parent_guide = $node->og_group_ref_documentation->entity;
while (!$sponsor_info_found && $parent_guide) {
if ($parent_guide->hasField('field_sponsor') && !$parent_guide->field_sponsor->isEmpty()) {
if ($parent_guide->hasField('field_sponsor_type') && $parent_guide->field_sponsor_type->value == "sponsor_all_nested_guides") {
$sponsor_info_found = TRUE;
$sponsor_data = $this->getSponsorData($parent_guide);
}
$parent_guide = NULL;
}
else {
$parent_guide = ($parent_guide->hasField('og_group_ref_documentation') && !$parent_guide->og_group_ref_documentation->isEmpty()) ? $parent_guide->og_group_ref_documentation->entity : NULL;
}
}
}
if (!$sponsor_info_found) {
return [];
}
}
return ['#theme' => 'drupalorg_sponsor_widget'] + $sponsor_data;
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {
if ($node = \Drupal::routeMatch()->getParameter('node')) {
return Cache::mergeTags(parent::getCacheTags(), ['node:' . $node->id()]);
}
return parent::getCacheTags();
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
return Cache::mergeContexts(parent::getCacheContexts(), ['route']);
}
/**
* Get the sponsor data given the documentation guide node as the argument.
*
* @param \Drupal\node\NodeInterface $node
* The documentation guide node.
*
* @return array
* An associative array containing the sponsor information.
*/
protected function getSponsorData(NodeInterface $node) {
$sponsor_data = $this->initSponsorData();
$logo_url = "";
if ($node->hasField('field_sponsor_alternative_url') && !$node->field_sponsor_alternative_url->isEmpty()) {
$organization_url = $node->get('field_sponsor_alternative_url')->uri;
$organization_url = Url::fromUri($organization_url)->toString();
}
else {
$organization_url = $node->field_sponsor->entity->get('field_link')->uri;
$organization_url = Url::fromUri($organization_url)->toString();
}
$organization_name = $node->field_sponsor->entity->getTitle();
$sponsor_data["#organization_name"] = $organization_name;
$sponsor_data["#organization_url"] = $organization_url;
if ($node->field_sponsor->entity->field_logo && $node->field_sponsor->entity->field_logo->entity) {
$media_entity = $node->field_sponsor->entity->field_logo->entity;
$file_id = $media_entity->getSource()->getSourceFieldValue($media_entity);
$logo_file = File::load($file_id);
$logo_url = ImageStyle::load("sponsor_widget_image")->buildUrl($logo_file->getFileUri());
}
$sponsor_data["#organization_logo"] = $logo_url;
return $sponsor_data;
}
/**
* Initializates the sponsor data array with NULL values.
*
* @return array
* An associative array with NULL values
*/
protected function initSponsorData() {
$sponsor_data = [
"#organization_name" => NULL,
'#organization_url' => NULL,
'#organization_logo' => NULL,
];
return $sponsor_data;
}
}
