YouTubeUrlFormatter.php in YouTube Field 8
File
src/Plugin/Field/FieldFormatter/YouTubeUrlFormatter.php
View source
<?php
namespace Drupal\youtube\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Url;
class YouTubeUrlFormatter extends FormatterBase {
public static function defaultSettings() {
return [
'link' => TRUE,
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$elements['link'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Output this field as a link'),
'#default_value' => $this
->getSetting('link'),
];
return $elements;
}
public function settingsSummary() {
$summary = [];
$link = $this
->getSetting('link');
if ($link) {
$summary[] = $this
->t('YouTube URL as a link.');
}
else {
$summary[] = $this
->t('YouTube URL as plain text.');
}
return $summary;
}
public function prepareView(array $entities_items) {
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
$link = $this
->getSetting('link');
foreach ($items as $delta => $item) {
if ($link) {
$element[$delta] = [
'#type' => 'link',
'#title' => $item->input,
'#url' => Url::fromUri($item->input),
'#options' => [
'attributes' => [
'class' => [
'youtube-url',
'youtube-url--' . Html::getClass($item->video_id),
],
],
'html' => TRUE,
],
];
}
else {
$element[$delta] = [
'#markup' => Html::escape($item->input),
];
}
}
return $element;
}
}