Advertisement
tabvn

Untitled

May 19th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.25 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\coursehunter\Plugin\Block;
  4.  
  5. use Drupal\Core\Block\BlockPluginInterface;
  6. use Drupal\Core\Block\BlockBase;
  7. use Drupal\node\Entity\Node;
  8.  
  9. /**
  10.  * Provides a 'OfficialAddress' block.
  11.  *
  12.  * @Block(
  13.  *  id = "OfficialAddress",
  14.  *  admin_label = @Translation("Official Address"),
  15.  * )
  16.  */
  17. class OfficialAddress extends BlockBase implements BlockPluginInterface {
  18.  
  19.  
  20.   public function getPhoneNumber($node) {
  21.  
  22.     if (!isset($node) || empty($node)) {
  23.       return "";
  24.     }
  25.  
  26.     return $node->get("field_phone_number")
  27.              ->getValue()[0]['value'];
  28.   }
  29.  
  30.   /**
  31.    * {@inheritdoc}
  32.    */
  33.   public function build() {
  34.  
  35.     $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  36.  
  37.     $activeNids = \Drupal::entityQuery('node')
  38.       ->condition('type', 'official_contact')
  39.       ->range(0, 1)
  40.       ->condition("langcode", $language)
  41.       ->execute();
  42.  
  43.  
  44.     $nid = NULL;
  45.     foreach ($activeNids as $k => $id) {
  46.       $nid = $id;
  47.       break;
  48.     }
  49.  
  50.     $activeNode = Node::load($nid);
  51.     $activeTitle = "";
  52.     $activePhoneNumber = $this->getPhoneNumber($activeNode);
  53.     if ($activeNode) {
  54.       $activeTitle = $activeNode->getTitle();
  55.     }
  56.  
  57.  
  58.     $nids = \Drupal::entityQuery('node')
  59.       ->condition('type', 'official_contact')
  60.       ->condition("langcode", $language, '<>')
  61.       ->execute();
  62.  
  63.     $nodes = Node::loadMultiple($nids);
  64.  
  65.     $list = [];
  66.     foreach ($nodes as $node) {
  67.       $title = $node->getTitle();
  68.       $contactPhone = $this->getPhoneNumber($node);
  69.       $list[] = [
  70.         "#markup" => "<span class=\"city\">$title</span><a href=\"tel:$contactPhone\">$contactPhone</a>",
  71.       ];
  72.     }
  73.  
  74.     $build['output'] = [
  75.       "#prefix"  => "<div class=\"branch-contact-information\">",
  76.       "#subffix" => "</div>",
  77.       '#markup'  => "<p><span>$activeTitle</span><a href=\"tel:$activePhoneNumber\">$activePhoneNumber</a></p>",
  78.       "children" => [
  79.         '#theme'      => 'item_list',
  80.         '#list_type'  => 'ul',
  81.         '#title'      => NULL,
  82.         '#items'      => $list,
  83.         '#attributes' => ['class' => 'official-phone-numbers'],
  84.       ],
  85.     ];
  86.  
  87.  
  88.     return $build;
  89.   }
  90.  
  91.  
  92.   public function getCacheMaxAge() {
  93.  
  94.     return 0;
  95.   }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement