cforge-2.0.x-dev/src/Plugin/Field/FieldFormatter/ContactMeFormatter.php

src/Plugin/Field/FieldFormatter/ContactMeFormatter.php
<?php

namespace Drupal\cforge\Plugin\Field\FieldFormatter;

use Drupal\cforge\Plugin\Field\FieldType\ContactMeItem;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Plugin implementation of the 'author' formatter.
 *
 * @FieldFormatter(
 *   id = "cf_contactme",
 *   label = @Translation("Contact me channels"),
 *   description = @Translation("Display emails, phone, url and social media handles."),
 *   field_types = {
 *     "cf_contactme"
 *   }
 * )
 */
class ContactMeFormatter extends FormatterBase {

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    // requires a lot of scaffolding code to inject into formatters.
    $privacy = \Drupal::service('user.data')->get('cforge', $items->getEntity()->id());

    $i = 0;
    // Put the definitive email if settings permit.
    if (!empty($privacy['show_email'])) {
      $elements[$i++] = [
        '#markup' => "<div class = \"channels email\">".$this->t('Email (definitive)').': '.$items->getEntity()->getEmail().'</div>',
        '#access' => \Drupal::service('user.data')->get('cforge', $items->getEntity()->id(), 'show_email'),
        '#weight' => $i
      ];
    }
    $available_channels = array_filter($items->getFieldDefinition()->getSetting('channels'));
    if (empty($privacy['show_phones'])) {
      unset($available_channels['tel'], $available_channels['mob'], $available_channels['whatsapp']);
    }
    foreach ($available_channels as $channel) {
      // Todo we should only show the properties enabled in widget settings
      if ($val = $items->{$channel}) {
        $title = new TranslatableMarkup(ContactMeItem::CHANNELS[$channel]['title']);
        $elements[$i++] = [
          '#markup' => '<div class = "channels '.$channel.'" title = "'.$title.'"> '.$items->{$channel}.'</div>',
          '#weight' => $i
        ];
      }
    }
    return $elements ? [
      '#title' => t('Contact details'),
      '#type' => 'details',
      '#open' => FALSE,
      '#attached' => ['library'=> ['cforge/social_media_icons']],
    ] + $elements : [];
  }

  function prepareView(array $entities_items) {
    parent::prepareView($entities_items);
    foreach ($entities_items as $item) {
      if ($item->altmail) {
        $url = ContactMeItem::CHANNELS['altmail']['prefix'] . $item->altmail;
        $item->altmail = '<a href="'.$url.'">'.$item->altmail.'</a>';
      }
      if ($item->whatsapp) { //  Must come before mob and tel
        $number = $item->{$item->whatsapp};
        $url = ContactMeItem::CHANNELS['whatsapp']['prefix'] . preg_replace('/^[0-9]/', '', $number);
        $item->whatsapp = '<a href="'.$url.'">'.$number.'</a>';
      }
      if ($item->tel) {
        if ($this->cansee($item->getEntity(), 'tel')) {
          $url = ContactMeItem::CHANNELS['tel']['prefix'] . preg_replace('/^[0-9]/', '', $item->tel);
          $item->tel = '<a href="'.$url.'">'.$item->tel.'</a>';
        }
        else {
          $item->tel = '';
        }
      }
      if ($item->mob) {
        $url = ContactMeItem::CHANNELS['mob']['prefix'] . preg_replace('/^[0-9]/', '', $item->mob);
        $item->mob = '<a href="'.$url.'">'.$item->mob.'</a>';
      }
      if ($item->web) {
        $url = ContactMeItem::CHANNELS['web']['prefix'] . $item->web;
        $item->web = '<a href="'.$url.'">'.$item->web.'</a>';
      }
      if ($item->twitter) {
        $url = ContactMeItem::CHANNELS['twitter']['prefix'] . $item->twitter;
        $item->twitter = '<a href="'.$url.'">'.$item->twitter.'</a>';
      }
      if ($item->telegram) {
        $url = ContactMeItem::CHANNELS['telegram']['prefix'] . $item->telegram;
        $item->telegram = '<a href="'.$url.'">'.$item->telegram.'</a>';
      }
      if ($item->instagram) {
        $url = ContactMeItem::CHANNELS['instagram']['prefix'] . $item->instagram;
        $item->instagram = '<a href="'.$url.'">'.$item->instagram.'</a>';
      }
      if ($item->linkedin) {
        $url = ContactMeItem::CHANNELS['linkedin']['prefix'] . $item->linkedin;
        $item->linkedin = '<a href="'.$url.'">'.$item->linkedin.'</a>';
      }
      if ($item->skype) {
        $url = ContactMeItem::CHANNELS['skype']['prefix'] . $item->skype . '?chat';
        $item->skype = '<a href="'.$url.'">'.$item->skype.'</a>';
      }
      if ($item->facebook) {
        $url = ContactMeItem::CHANNELS['facebook']['prefix'] . $item->facebook;
        $item->facebook = '<a href="'.$url.'">'.$item->facebook.'</a>';
      }
    }
  }

  /**
   * Check the user's privacy settings on the given property.
   * @param type $property
   */
  function canSee($owner, $property) : bool {
    if (\Drupal::currentUser()->hasPermission('administer users') or
      \Drupal::currentUser()->id() == $owner->id()) {
      return TRUE;
    }
    switch($property) {
      case 'tel':
      case 'mob':
        $key = 'show_phones';
        break;
      case 'email':
        $key = 'show_email'; break;
    }
    $stored = \Drupal::service('user.data')->get('cforge', $owner->id(), $key);
    return (bool)$stored;
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc