diff --git a/drupalorg.module b/drupalorg.module index 968c9bf0963b343ffb5717ca0ffecc1b94b1c201..0fcd02776c00d68be4bbb17befdadbb195ca5778 100644 --- a/drupalorg.module +++ b/drupalorg.module @@ -58,6 +58,14 @@ function drupalorg_theme() { 'template' => 'drupalorg-issue-forks-management', 'file' => 'drupalorg.theme.inc', ], + 'drupalorg_sponsor_widget' => [ + 'variables' => [ + 'organization_name' => NULL, + 'organization_url' => NULL, + 'organization_logo' => NULL, + ], + 'template' => 'drupalorg-sponsor-widget', + ], ]; } diff --git a/src/Plugin/Block/DrupalOrgSponsorWidget.php b/src/Plugin/Block/DrupalOrgSponsorWidget.php new file mode 100644 index 0000000000000000000000000000000000000000..fe4ae0dd866ab22134c1ec7f959296b36b0a9c2d --- /dev/null +++ b/src/Plugin/Block/DrupalOrgSponsorWidget.php @@ -0,0 +1,173 @@ +<?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->hasField('field_sponsor')) { + return []; + } + + if (!$node->field_sponsor->isEmpty()) { + $sponsor_data = $this->getSponsorData($node); + } + else { + $sponsor_data = $this->initSponsorData(); + 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; + $sponsor_info_found = FALSE; + 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; + } + } + } + } + + 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()]); + } + else { + 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 name, link to the sponsor's site and a link to the logo + */ + 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; + } +} diff --git a/templates/drupalorg-sponsor-widget.html.twig b/templates/drupalorg-sponsor-widget.html.twig new file mode 100644 index 0000000000000000000000000000000000000000..8b60585f2b2d2a83ff348f07632d98cf8d3e6f8d --- /dev/null +++ b/templates/drupalorg-sponsor-widget.html.twig @@ -0,0 +1,21 @@ +{# + Available variables: + - organization_url: Organization URL (Internal/External URL). + - organization_name: Organization Name. + - organization_logo: URL to the organization logo. +#} +{{ attach_library('bluecheese/documentation_guide') }} + +{% if organization_url is not null and organization_name is not null %} + <div class='organization-widget'> + <p>{{ "This document has been adopted by :"|t }}</p> + <div class='child-link'> + {% if organization_logo %} + <a href="{{ organization_url }}"><img src="{{ organization_logo }}" alt="{{ organization_name }}" /></a> + {% else %} + <a href="{{ organization_url }}">{{ organization_name }}</a> + {% endif %} + </div> + <div class="background-shadow"></div> + </div> +{% endif %}