Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Drupal\coursehunter\Plugin\Block;
- use Drupal\Core\Block\BlockPluginInterface;
- use Drupal\Core\Block\BlockBase;
- use Drupal\node\Entity\Node;
- /**
- * Provides a 'OfficialAddress' block.
- *
- * @Block(
- * id = "OfficialAddress",
- * admin_label = @Translation("Official Address"),
- * )
- */
- class OfficialAddress extends BlockBase implements BlockPluginInterface {
- public function getPhoneNumber($node) {
- if (!isset($node) || empty($node)) {
- return "";
- }
- return $node->get("field_phone_number")
- ->getValue()[0]['value'];
- }
- /**
- * {@inheritdoc}
- */
- public function build() {
- $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
- $activeNids = \Drupal::entityQuery('node')
- ->condition('type', 'official_contact')
- ->range(0, 1)
- ->condition("langcode", $language)
- ->execute();
- $nid = NULL;
- foreach ($activeNids as $k => $id) {
- $nid = $id;
- break;
- }
- $activeNode = Node::load($nid);
- $activeTitle = "";
- $activePhoneNumber = $this->getPhoneNumber($activeNode);
- if ($activeNode) {
- $activeTitle = $activeNode->getTitle();
- }
- $nids = \Drupal::entityQuery('node')
- ->condition('type', 'official_contact')
- ->condition("langcode", $language, '<>')
- ->execute();
- $nodes = Node::loadMultiple($nids);
- $list = [];
- foreach ($nodes as $node) {
- $title = $node->getTitle();
- $contactPhone = $this->getPhoneNumber($node);
- $list[] = [
- "#markup" => "<span class=\"city\">$title</span><a href=\"tel:$contactPhone\">$contactPhone</a>",
- ];
- }
- $build['output'] = [
- "#prefix" => "<div class=\"branch-contact-information\">",
- "#subffix" => "</div>",
- '#markup' => "<p><span>$activeTitle</span><a href=\"tel:$activePhoneNumber\">$activePhoneNumber</a></p>",
- "children" => [
- '#theme' => 'item_list',
- '#list_type' => 'ul',
- '#title' => NULL,
- '#items' => $list,
- '#attributes' => ['class' => 'official-phone-numbers'],
- ],
- ];
- return $build;
- }
- public function getCacheMaxAge() {
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement