class ImageWidget

Plugin implementation of the 'image_image' widget.

Plugin annotation


@Plugin(
  id = "image_image",
  module = "image",
  label = @Translation("Image"),
  field_types = {
    "image"
  },
  settings = {
    "progress_indicator" = "throbber",
    "preview_image_style" = "thumbnail",
  },
  default_value = FALSE
)

Hierarchy

Expanded class hierarchy of ImageWidget

File

drupal/core/modules/image/lib/Drupal/image/Plugin/field/widget/ImageWidget.php, line 33
Contains \Drupal\image\Plugin\field\widget\ImageWidget.

Namespace

Drupal\image\Plugin\field\widget
View source
class ImageWidget extends FileWidget {

  /**
   * Overrides \Drupal\file\Plugin\field\widget\FileWidget::settingsForm().
   */
  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;
  }

  /**
   * Overrides \Drupal\file\Plugin\field\widget\FileWidget::formMultipleElements().
   *
   * Special handling for draggable multiple widgets and 'add more' button.
   */
  protected function formMultipleElements(EntityInterface $entity, array $items, $langcode, array &$form, array &$form_state) {
    $elements = parent::formMultipleElements($entity, $items, $langcode, $form, $form_state);
    if ($this->field['cardinality'] == 1) {

      // If there's only one field, return it as delta 0.
      if (empty($elements[0]['#default_value']['fid'])) {
        $elements[0]['#description'] = theme('file_upload_help', array(
          'description' => $this->instance['description'],
          'upload_validators' => $elements[0]['#upload_validators'],
        ));
      }
    }
    else {
      $elements['#file_upload_description'] = theme('file_upload_help', array(
        'upload_validators' => $elements[0]['#upload_validators'],
      ));
    }
    return $elements;
  }

  /**
   * Overrides \Drupal\file\Plugin\field\widget\FileWidget::formElement().
   */
  public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
    $element = parent::formElement($items, $delta, $element, $langcode, $form, $form_state);
    $settings = $this->instance['settings'];

    // Add upload resolution validation.
    if ($settings['max_resolution'] || $settings['min_resolution']) {
      $element['#upload_validators']['file_validate_image_resolution'] = array(
        $settings['max_resolution'],
        $settings['min_resolution'],
      );
    }

    // 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');
    $element['#title_field'] = $settings['title_field'];
    $element['#alt_field'] = $settings['alt_field'];
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ImageWidget::formElement public function Overrides \Drupal\file\Plugin\field\widget\FileWidget::formElement(). Overrides FileWidget::formElement
ImageWidget::formMultipleElements protected function Overrides \Drupal\file\Plugin\field\widget\FileWidget::formMultipleElements(). Overrides FileWidget::formMultipleElements
ImageWidget::settingsForm public function Overrides \Drupal\file\Plugin\field\widget\FileWidget::settingsForm(). Overrides FileWidget::settingsForm
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$discovery protected property The discovery object.
PluginBase::$plugin_id protected property The plugin_id.
PluginBase::getDefinition public function Implements Drupal\Component\Plugin\PluginInterface::getDefinition(). Overrides PluginInspectionInterface::getDefinition
PluginBase::getPluginId public function Implements Drupal\Component\Plugin\PluginInterface::getPluginId(). Overrides PluginInspectionInterface::getPluginId
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::getDefaultSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getDefaultSettings(). Overrides PluginSettingsInterface::getDefaultSettings
PluginSettingsBase::getSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSetting(). Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSettings(). Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::setSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSetting(). Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSettings(). Overrides PluginSettingsInterface::setSettings
WidgetBase::$field protected property The field definition.
WidgetBase::$instance protected property The field instance definition.
WidgetBase::$settings protected property The widget settings. Overrides PluginSettingsBase::$settings
WidgetBase::$weight protected property The widget weight.
WidgetBase::errorElement public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::errorElement(). Overrides WidgetInterface::errorElement 4
WidgetBase::flagErrors public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::flagErrors(). Overrides WidgetBaseInterface::flagErrors 1
WidgetBase::form public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::form(). Overrides WidgetBaseInterface::form
WidgetBase::formSingleElement protected function Generates the form element for a single copy of the widget.
WidgetBase::massageFormValues public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::massageFormValues() Overrides WidgetInterface::massageFormValues 1
WidgetBase::sortItems protected function Sorts submitted field values according to drag-n-drop reordering.
WidgetBase::submit public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::submit(). Overrides WidgetBaseInterface::submit
WidgetBase::__construct public function Constructs a WidgetBase object. Overrides PluginBase::__construct