/** * {@inheritdoc} */ protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) { $elements = parent::formMultipleElements($items, $form, $form_state); // Alter #upload_validators passed to #file_upload_description. $this->alterFileUploadHelpParameters($elements['#file_upload_description']); // TODO: Why do we need this? if ($this->isDefaultValueWidget($form_state) && !empty($elements[1])) { unset($elements[1]); $elements['#file_upload_delta'] = 0; } return $elements; }
/** * Form API callback: Processes a image_image field element. * * Expands the image_image type to include the alt and title fields. * * This method is assigned as a #process callback in formElement() method. */ public static function process($element, FormStateInterface $form_state, $form) { $item = $element['#value']; $item['fids'] = $element['fids']['#value']; $element['#theme'] = 'image_widget'; $element['#attached']['css'][] = drupal_get_path('module', 'image') . '/css/image.theme.css'; // Add the image preview. if (!empty($element['#files']) && $element['#preview_image_style']) { $file = reset($element['#files']); $variables = array('style_name' => $element['#preview_image_style'], 'uri' => $file->getFileUri()); // Determine image dimensions. if (isset($element['#value']['width']) && isset($element['#value']['height'])) { $variables['width'] = $element['#value']['width']; $variables['height'] = $element['#value']['height']; } else { $image = \Drupal::service('image.factory')->get($file->getFileUri()); if ($image->isValid()) { $variables['width'] = $image->getWidth(); $variables['height'] = $image->getHeight(); } else { $variables['width'] = $variables['height'] = NULL; } } $element['preview'] = array('#theme' => 'image_style', '#width' => $variables['width'], '#height' => $variables['height'], '#style_name' => $variables['style_name'], '#uri' => $variables['uri']); // Store the dimensions in the form so the file doesn't have to be // accessed again. This is important for remote files. $element['width'] = array('#type' => 'hidden', '#value' => $variables['width']); $element['height'] = array('#type' => 'hidden', '#value' => $variables['height']); } // Add the additional alt and title fields. $element['alt'] = array('#title' => t('Alternate text'), '#type' => 'textfield', '#default_value' => isset($item['alt']) ? $item['alt'] : '', '#description' => t('This text will be used by screen readers, search engines, or when the image cannot be loaded.'), '#maxlength' => 512, '#weight' => -2, '#access' => (bool) $item['fids'] && $element['#alt_field'], '#element_validate' => $element['#alt_field_required'] == 1 ? array(array(get_called_class(), 'validateRequiredFields')) : array()); $element['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#default_value' => isset($item['title']) ? $item['title'] : '', '#description' => t('The title is used as a tool tip when the user hovers the mouse over the image.'), '#maxlength' => 1024, '#weight' => -1, '#access' => (bool) $item['fids'] && $element['#title_field'], '#element_validate' => $element['#title_field_required'] == 1 ? array(array(get_called_class(), 'validateRequiredFields')) : array()); return parent::process($element, $form_state, $form); }
/** * {@inheritdoc} */ public function onDependencyRemoval(array $dependencies) { $changed = parent::onDependencyRemoval($dependencies); $style_id = $this->getSetting('preview_image_style'); /** @var \Drupal\image\ImageStyleInterface $style */ if ($style_id && ($style = ImageStyle::load($style_id))) { if (!empty($dependencies[$style->getConfigDependencyKey()][$style->getConfigDependencyName()])) { /** @var \Drupal\image\ImageStyleStorageInterface $storage */ $storage = \Drupal::entityManager()->getStorage($style->getEntityTypeId()); $replacement_id = $storage->getReplacementId($style_id); // If a valid replacement has been provided in the storage, replace the // preview image style with the replacement. if ($replacement_id && ImageStyle::load($replacement_id)) { $this->setSetting('preview_image_style', $replacement_id); } else { $this->setSetting('preview_image_style', ''); } // Signal that the formatter plugin settings were updated. $changed = TRUE; } } return $changed; }
/** * Form API callback: Processes a video_upload field element. * * This method is assigned as a #process callback in formElement() method. */ public static function process($element, FormStateInterface $form_state, $form) { return parent::process($element, $form_state, $form); }
/** * Form API callback: Processes a file_generic field element. * * Expands the file_generic type to include the description and display * fields. * * This method is assigned as a #process callback in formElement() method. */ public static function process($element, FormStateInterface $form_state, $form) { $element = parent::process($element, $form_state, $form); $item = $element['#value']; $element['data']['#value'] = $item['data']; $element['data']['#type'] = 'hidden'; return $element; }