Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Drupal\youtube_video_formatter\Plugin\Field\FieldFormatter;
- use Drupal\Core\Field\FieldItemListInterface;
- use Drupal\Core\Field\FormatterBase;
- use Drupal\Core\Form\FormStateInterface;
- /**
- * Plugin implementation of the 'YouTube Video' formatter.
- *
- * @FieldFormatter(
- * id = "youtube_video_formatter",
- * label = @Translation("YouTube Video"),
- * field_types = {
- * "string"
- * }
- * )
- */
- class YoutubeFieldFormatter extends FormatterBase {
- // /**
- // * {@inheritdoc}
- // */
- public function settingsSummary() {
- $summary = [
- 'title' => 'Settings:',
- 'Width' => t('Width: ').$this->getSetting('vwidth'),
- 'Height' => t('Height: ').$this->getSetting('vheight'),
- 'Auto play' => t('Autoplay:').$this->getSetting('autoplayon'),
- ];
- return $summary;
- }
- /**
- * {@inheritdoc}
- */
- public static function defaultSettings() {
- return [
- 'vwidth' => '560',
- 'vheight' => '325',
- 'autoplayon' => '0',
- ] + parent::defaultSettings();
- }
- /**
- * {@inheritdoc}
- */
- public function settingsForm(array $form, FormStateInterface $form_state) {
- $element = [];
- // kint($form);
- $element['vwidth'] = [
- '#title' => t('Video Width'),
- '#type' => 'textfield',
- // '#default_value' => $this->getSetting('vwidth'),
- ];
- $element['vheight'] = [
- '#title' => t('Video Height'),
- '#type' => 'textfield',
- // '#default_value' => $this->getSetting('vheight'),
- ];
- $element['autoplayon'] = [
- '#title' => t('Autoplay on?'),
- '#type' => 'checkbox',
- // '#default_value' => $this->getSetting('autoplayon'),
- ];
- return $element;
- }
- /**
- * {@inheritdoc}
- */
- public function viewElements(FieldItemListInterface $items, $langcode) {
- $element = [];
- foreach ($items as $delta => $item) {
- $element[$delta] = [
- '#theme' => 'youtube_video',
- '#youtube_id' => $item->value,
- '#vheight' => $this->getSetting('vheight'),
- '#vwidth' => $this->getSetting('vwidth'),
- '#autoplayon' => $this->getSetting('autoplayon'),
- ];
- }
- return $element;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement