You are here

public function FacebookCommentsFormatter::viewElements in Facebook Comments Social Plugin 8

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/FacebookCommentsFormatter.php, line 69

Class

FacebookCommentsFormatter
Plugin implementation of the 'facebook_comments' formatter.

Namespace

Drupal\facebook_comments\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = array();
  $style = $this
    ->getSetting('facebook_comments_style');
  $width = $this
    ->getSetting('facebook_comments_width');
  $fluid = $this
    ->getSetting('facebook_comments_width_fluid');
  $amount = $this
    ->getSetting('facebook_comments_amount');
  $config = \Drupal::config('facebook_comments.settings');
  $appid = $config
    ->get('facebook_comments_appid');
  $admins = $config
    ->get('facebook_comments_admins');
  $ssl = $config
    ->get('facebook_comments_ssl');
  $options = array(
    'absolute' => TRUE,
  );
  $url = Url::fromRoute('<current>', array(), $options)
    ->toString();
  $lang = _facebook_comments_get_language_code();

  // If the path is non-SSL, rewrite it to SSL.
  if ($ssl && strpos($url, "http://") !== FALSE) {
    $url = str_ireplace("http://", "https://", $url);
  }
  if ($fluid) {
    $class = "fb-comments-fluid";
  }
  else {
    $class = "";
  }
  $output = array(
    '#theme' => 'facebook_comments_field',
    '#style' => $style,
    '#amount' => $amount,
    '#width' => $width,
    '#class' => $class,
    '#url' => $url,
    '#lang' => $lang,
  );

  // Display Facebook comments with fluid width
  if ($fluid) {
    $output['#attached']['library'][] = 'facebook_comments/fluid';
  }

  // Add the Facebook App ID if it exists
  if ($appid) {
    $a = array(
      '#tag' => 'meta',
      '#attributes' => array(
        'property' => 'fb:app_id',
        'content' => $appid,
      ),
    );
    $output['#attached']['html_head'][] = [
      $a,
      'facebook_comments',
    ];
  }
  elseif ($admins) {
    $admin = explode(",", $admins);
    foreach ($admin as $key => $value) {
      $a = array(
        '#tag' => 'meta',
        '#attributes' => array(
          'property' => 'fb:admins',
          'content' => trim($value),
        ),
      );
      $output['#attached']['html_head'][] = [
        $a,
        'facebook_comments_' . $key,
      ];
    }
  }
  $elements[] = $output;
  return $elements;
}