Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\youtube_video_formatter\Plugin\Field\FieldFormatter;
  4.  
  5. use Drupal\Core\Field\FieldItemListInterface;
  6. use Drupal\Core\Field\FormatterBase;
  7. use Drupal\Core\Form\FormStateInterface;
  8.  
  9.  
  10. /**
  11. * Plugin implementation of the 'YouTube Video' formatter.
  12. *
  13. * @FieldFormatter(
  14. * id = "youtube_video_formatter",
  15. * label = @Translation("YouTube Video"),
  16. * field_types = {
  17. * "string"
  18. * }
  19. * )
  20. */
  21. class YoutubeFieldFormatter extends FormatterBase {
  22. // /**
  23. // * {@inheritdoc}
  24. // */
  25. public function settingsSummary() {
  26. $summary = [
  27. 'title' => 'Settings:',
  28. 'Width' => t('Width: ').$this->getSetting('vwidth'),
  29. 'Height' => t('Height: ').$this->getSetting('vheight'),
  30. 'Auto play' => t('Autoplay:').$this->getSetting('autoplayon'),
  31.  
  32. ];
  33. return $summary;
  34. }
  35.  
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public static function defaultSettings() {
  40. return [
  41. 'vwidth' => '560',
  42. 'vheight' => '325',
  43. 'autoplayon' => '0',
  44. ] + parent::defaultSettings();
  45. }
  46.  
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function settingsForm(array $form, FormStateInterface $form_state) {
  51. $element = [];
  52. // kint($form);
  53. $element['vwidth'] = [
  54. '#title' => t('Video Width'),
  55. '#type' => 'textfield',
  56. // '#default_value' => $this->getSetting('vwidth'),
  57. ];
  58.  
  59. $element['vheight'] = [
  60. '#title' => t('Video Height'),
  61. '#type' => 'textfield',
  62. // '#default_value' => $this->getSetting('vheight'),
  63. ];
  64. $element['autoplayon'] = [
  65. '#title' => t('Autoplay on?'),
  66. '#type' => 'checkbox',
  67. // '#default_value' => $this->getSetting('autoplayon'),
  68. ];
  69.  
  70.  
  71. return $element;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function viewElements(FieldItemListInterface $items, $langcode) {
  77. $element = [];
  78. foreach ($items as $delta => $item) {
  79. $element[$delta] = [
  80. '#theme' => 'youtube_video',
  81. '#youtube_id' => $item->value,
  82. '#vheight' => $this->getSetting('vheight'),
  83. '#vwidth' => $this->getSetting('vwidth'),
  84. '#autoplayon' => $this->getSetting('autoplayon'),
  85. ];
  86. }
  87.  
  88. return $element;
  89. }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement