syn_promo-8.x-1.3/src/Plugin/Block/PromoBlock.php
src/Plugin/Block/PromoBlock.php
<?php
namespace Drupal\syn_promo\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a promo event block.
*
* @Block(
* id = "promo_block",
* admin_label = @Translation("Promo event"),
* category = @Translation("Custom"),
* )
*/
final class PromoBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build(): array {
$data = $this->getData();
$build = [
'#theme' => 'promo_block',
'#data' => $data,
'#attached' => [
'drupalSettings' => [
'promo-event' => $data['delay'] ?? 0,
],
],
'#cache' => ['contexts' => ['url', 'theme', 'languages', 'user.permissions']],
];
return $build;
}
/**
* Returns data for the block.
*/
private function getData() {
$type = '';
if (is_object($entity = \Drupal::routeMatch()->getParameter('node'))) {
$type = 'node_';
$bundle = $entity->bundle();
$data['node'] = $entity->id();
}
elseif (is_object($entity = \Drupal::routeMatch()->getParameter('taxonomy_term'))) {
$type = 'term_';
$bundle = $entity->bundle();
$data['node'] = $entity->id();
}
elseif (is_object($entity = \Drupal::routeMatch()->getParameter('commerce_product'))) {
$type = 'product_';
$data['node'] = $entity->id();
}
$nids = [];
$query = \Drupal::entityQuery('node');
$query->accessCheck(TRUE);
$query->condition('type', 'syn_promo');
$query->condition('status', 1);
if (is_object($entity)) {
$type_name = mb_substr($type, 0, -1);
$node_group = $query->andConditionGroup()
->condition('field_promo_target_' . $type_name, $entity->id())
->condition('field_promo_visibility', $type_name);
}
$or_group = $query->orConditionGroup();
$or_group->condition('field_promo_visibility', 'all');
if (is_object($entity) && $type != 'product_') {
$or_group->condition('field_promo_target_type', $type . $bundle);
$or_group->condition($node_group);
}
elseif (is_object($entity) && $type == 'product_') {
$or_group->condition($node_group);
}
$query->condition($or_group);
$nids = $query->execute();
if (!empty($nids)) {
$config = \Drupal::config('synlanding.settings');
$data['layout'] = $config->get('design');
foreach ($nids as $nid) {
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
$start_date = $node->field_promo_start_date->value;
$out_date = $node->field_promo_finish->value;
$current_date = time();
if ($current_date > $start_date && $current_date < $out_date) {
if (!$node->field_promo_delay->isEmpty()) {
$data['delay'] = $node->field_promo_delay->value;
}
if ($node->field_promo_visibility->value == 'node') {
$data['context'] = '';
}
$data['nodes'][$node->id()] = [
'id' => $node->id(),
'title' => $node->getTitle(),
'description' => $node->body->value,
'button' => $this->getButtonData($node),
'color' => $this->getColors($node),
];
}
}
return $data;
}
return [];
}
/**
* Returns button data.
*/
private function getButtonData($node) {
$data = [];
$button_entity = $node->field_promo_button->entity;
if ($button_entity) {
$data = [
'title' => $button_entity->field_title->value ?? '',
'link' => !$button_entity->field_link->isEmpty() ? $button_entity->field_link[0]->getUrl()->toString() : '',
'target' => $button_entity->field_blank->value == '1' ? '_blank' : '',
'modal' => $button_entity->field_form_id->value ?? '',
];
}
return $data;
}
/**
*
*/
private function getColors($node) {
$color = [];
if ($node->field_promo_custom->value) {
$color['text'] = $node->field_promo_text_color->getValue()[0]['color'];
$color['background'] = $node->field_promo_background->getValue()[0]['color'];
}
return $color;
}
}
