ViewsWidget.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace Drupal\paragraphs_views\Plugin\Field\FieldWidget;
  3. use Drupal\Core\Field\FieldItemListInterface;
  4. use Drupal\Core\Field\WidgetBase;
  5. use Drupal\Core\Form\FormStateInterface;
  6. /**
  7. * Plugin implementation of the 'paragraphs_view_widget' widget.
  8. *
  9. * @FieldWidget(
  10. * id = "paragraphs_view_widget",
  11. * label = @Translation("Views widget"),
  12. * field_types = {
  13. * "entity_reference"
  14. * }
  15. * )
  16. */
  17. class ViewsWidget extends WidgetBase {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public static function defaultSettings() {
  22. return array(
  23. 'size' => 60,
  24. 'placeholder' => '',
  25. ) + parent::defaultSettings();
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function settingsForm(array $form, FormStateInterface $form_state) {
  31. $elements = [];
  32. $elements['size'] = array(
  33. '#type' => 'number',
  34. '#title' => t('Size of textfield'),
  35. '#default_value' => $this->getSetting('size'),
  36. '#required' => TRUE,
  37. '#min' => 1,
  38. );
  39. $elements['placeholder'] = array(
  40. '#type' => 'textfield',
  41. '#title' => t('Placeholder'),
  42. '#default_value' => $this->getSetting('placeholder'),
  43. '#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
  44. );
  45. return $elements;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function settingsSummary() {
  51. $summary = [];
  52. $summary[] = t('Textfield size: !size', array('!size' => $this->getSetting('size')));
  53. if (!empty($this->getSetting('placeholder'))) {
  54. $summary[] = t('Placeholder: @placeholder', array('@placeholder' => $this->getSetting('placeholder')));
  55. }
  56. return $summary;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
  62. $element = [];
  63. $element['value'] = $element + array(
  64. '#type' => 'textfield',
  65. '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
  66. '#size' => $this->getSetting('size'),
  67. '#placeholder' => $this->getSetting('placeholder'),
  68. '#maxlength' => $this->getFieldSetting('max_length'),
  69. );
  70. return $element;
  71. }
  72. }