TwitterFormatterBase.php in Twitter Embed 8
File
src/Plugin/Field/FieldFormatter/TwitterFormatterBase.php
View source
<?php
namespace Drupal\twitter_embed\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
abstract class TwitterFormatterBase extends FormatterBase {
protected $twitterWidget;
public function settingsForm(array $form, FormStateInterface $form_state) {
$settingsForm = $this->twitterWidget
->getSettingsForm($this
->getSettings());
unset($settingsForm['display_options']);
$selector = 'fields[' . $this->fieldDefinition
->getName() . '][settings_edit_form][settings]';
$settingsFormWithStates = $this->twitterWidget
->setSettingsFormStates($settingsForm, $selector);
return $settingsFormWithStates + parent::settingsForm($form, $form_state);
}
public function settingsSummary() {
$summary = [];
$summary[] = $this
->t('Display style: @display_style', [
'@display_style' => $this
->getSettings()['display_style'],
]);
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$configuration = $this
->getSettings();
$configuration['username'] = $this
->viewValue($item);
$elements[$delta] = $this->twitterWidget
->getWidget($configuration);
}
return $elements;
}
protected function viewValue(FieldItemInterface $item) {
return nl2br(Html::escape($item->value));
}
}