TwitterEmbedFormatter.php in Media entity Twitter 8.2
File
src/Plugin/Field/FieldFormatter/TwitterEmbedFormatter.php
View source
<?php
namespace Drupal\media_entity_twitter\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\media_entity_twitter\Plugin\media\Source\Twitter;
class TwitterEmbedFormatter extends FormatterBase {
public static function defaultSettings() {
$settings['conversation'] = FALSE;
return $settings;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form['conversation'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show previous tweet?'),
'#description' => $this
->t('If the requested tweet was a reply to another one, this option will display a summary of the previous tweet too. By default only the requested tweet will be displayed.'),
'#default_value' => $this
->getSetting('conversation'),
];
return $form;
}
public function settingsSummary() {
$summary = [];
if ($this
->getSetting('conversation')) {
$summary[] = $this
->t('Show previous tweet, if applicable.');
}
else {
$summary[] = $this
->t('Do not show previous tweet, if applicable.');
}
return $summary;
}
protected function getEmbedCode(FieldItemInterface $item) {
switch ($item
->getFieldDefinition()
->getType()) {
case 'link':
return $item->uri;
case 'string':
case 'string_long':
return $item->value;
default:
break;
}
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
foreach ($items as $delta => $item) {
$matches = [];
foreach (Twitter::$validationRegexp as $pattern => $key) {
if (preg_match($pattern, $this
->getEmbedCode($item), $item_matches)) {
$matches[] = $item_matches;
}
}
if (!empty($matches)) {
$matches = reset($matches);
}
if (!empty($matches['user']) && !empty($matches['id'])) {
$element[$delta] = [
'#theme' => 'media_entity_twitter_tweet',
'#path' => 'https://twitter.com/' . $matches['user'] . '/statuses/' . $matches['id'],
'#attributes' => [
'class' => [
'twitter-tweet',
'element-hidden',
],
'lang' => 'en',
],
];
if (!$this
->getSetting('conversation')) {
$element[$delta]['#attributes']['data-conversation'] = 'none';
}
}
}
if (!empty($element)) {
$element['#attached'] = [
'library' => [
'media_entity_twitter/integration',
],
];
}
return $element;
}
}