ImageWidget.php 4.45 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Contains \Drupal\image\Plugin\Field\FieldWidget\ImageWidget.
6
7
 */

8
namespace Drupal\image\Plugin\Field\FieldWidget;
9

10
use Drupal\file\Plugin\Field\FieldWidget\FileWidget;
11
use Drupal\Core\Field\FieldItemListInterface;
12
13
14
15

/**
 * Plugin implementation of the 'image_image' widget.
 *
16
 * @FieldWidget(
17
18
19
20
21
22
23
24
 *   id = "image_image",
 *   label = @Translation("Image"),
 *   field_types = {
 *     "image"
 *   },
 *   settings = {
 *     "progress_indicator" = "throbber",
 *     "preview_image_style" = "thumbnail",
25
 *   }
26
27
28
29
30
 * )
 */
class ImageWidget extends FileWidget {

  /**
31
   * {@inheritdoc}
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
   */
  public function settingsForm(array $form, array &$form_state) {
    $element = parent::settingsForm($form, $form_state);

    $element['preview_image_style'] = array(
      '#title' => t('Preview image style'),
      '#type' => 'select',
      '#options' => image_style_options(FALSE),
      '#empty_option' => '<' . t('no preview') . '>',
      '#default_value' => $this->getSetting('preview_image_style'),
      '#description' => t('The preview image will be shown while editing the content.'),
      '#weight' => 15,
    );

    return $element;
  }

  /**
50
   * {@inheritdoc}
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
   */
  public function settingsSummary() {
    $summary = parent::settingsSummary();

    $image_styles = image_style_options(FALSE);
    // Unset possible 'No defined styles' option.
    unset($image_styles['']);
    // Styles could be lost because of enabled/disabled modules that defines
    // their styles in code.
    $image_style_setting = $this->getSetting('preview_image_style');
    if (isset($image_styles[$image_style_setting])) {
      $preview_image_style = t('Preview image style: @style', array('@style' => $image_styles[$image_style_setting]));
    }
    else {
      $preview_image_style = t('Original image');
    }

    array_unshift($summary, $preview_image_style);

    return $summary;
  }

  /**
74
   * Overrides \Drupal\file\Plugin\Field\FieldWidget\FileWidget::formMultipleElements().
75
76
77
   *
   * Special handling for draggable multiple widgets and 'add more' button.
   */
78
  protected function formMultipleElements(FieldItemListInterface $items, array &$form, array &$form_state) {
79
    $elements = parent::formMultipleElements($items, $form, $form_state);
80

81
    $cardinality = $this->fieldDefinition->getCardinality();
82
83
    $file_upload_help = array(
      '#theme' => 'file_upload_help',
84
      '#description' => '',
85
86
87
      '#upload_validators' => $elements[0]['#upload_validators'],
      '#cardinality' => $cardinality,
    );
88
    if ($cardinality == 1) {
89
      // If there's only one field, return it as delta 0.
90
      if (empty($elements[0]['#default_value']['fids'])) {
91
        $file_upload_help['#description'] = field_filter_xss($this->fieldDefinition->getDescription());
92
        $elements[0]['#description'] = drupal_render($file_upload_help);
93
94
95
      }
    }
    else {
96
      $elements['#file_upload_description'] = $file_upload_help;
97
98
99
100
101
102
    }

    return $elements;
  }

  /**
103
   * {@inheritdoc}
104
   */
105
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state) {
106
    $element = parent::formElement($items, $delta, $element, $form, $form_state);
107

108
    $field_settings = $this->getFieldSettings();
109
110

    // Add upload resolution validation.
111
112
    if ($field_settings['max_resolution'] || $field_settings['min_resolution']) {
      $element['#upload_validators']['file_validate_image_resolution'] = array($field_settings['max_resolution'], $field_settings['min_resolution']);
113
114
115
116
117
118
119
120
121
122
123
124
    }

    // If not using custom extension validation, ensure this is an image.
    $supported_extensions = array('png', 'gif', 'jpg', 'jpeg');
    $extensions = isset($element['#upload_validators']['file_validate_extensions'][0]) ? $element['#upload_validators']['file_validate_extensions'][0] : implode(' ', $supported_extensions);
    $extensions = array_intersect(explode(' ', $extensions), $supported_extensions);
    $element['#upload_validators']['file_validate_extensions'][0] = implode(' ', $extensions);

    // Add all extra functionality provided by the image widget.
    $element['#process'][] = 'image_field_widget_process';
    // Add properties needed by image_field_widget_process().
    $element['#preview_image_style'] = $this->getSetting('preview_image_style');
125
126
127
    $element['#title_field'] = $field_settings['title_field'];
    $element['#alt_field'] = $field_settings['alt_field'];
    $element['#alt_field_required'] = $field_settings['alt_field_required'];
128
129
130
131
132

    return $element;
  }

}