/** * {@inheritdoc} */ public static function process($element, FormStateInterface $form_state, $form) { $item = $element['#value']; $element['slide_text'] = array('#type' => 'textarea', '#title' => t('Slide Text'), '#default_value' => isset($element['#default_value']['slide_text']) ? $element['#default_value']['slide_text'] : '', '#maxlength' => 1024, '#weight' => -2); $element = parent::process($element, $form_state, $form); return $element; }
/** * Overrides * \Drupal\file\Plugin\Field\FieldWidget\FileWidget::formMultipleElements(). * * Special handling for draggable multiple widgets and 'add more' button. * * @param FieldItemListInterface $items * @param array $form * @param FormStateInterface $form_state * @return array */ protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) { $elements = parent::formMultipleElements($items, $form, $form_state); $elements['#theme'] = 'nice_imagefield_widget_multiple'; $elements['#attached']['library'][] = 'nice_imagefield_widget/sortable'; return $elements; }
/** * {@inheritDocs} * * Form API callback. Retrieves the value for the file_generic field element. * * This method is assigned as a #value_callback in formElement() method. */ public static function value($element, $input = FALSE, FormStateInterface $form_state) { $return = parent::value($element, $input, $form_state); // When an element is loaded, focal_point needs to be set. During a form // submission the value will already be there. if (isset($return['target_id']) && !isset($return['focal_point'])) { $element['#focal_point'] = new FocalPoint($return['target_id']); $return['focal_point'] = $element['#focal_point']->getFocalPoint(); } return $return; }
/** * {@inheritdoc} * * Form API callback. Retrieves the value for the file_generic field element. * * This method is assigned as a #value_callback in formElement() method. */ public static function value($element, $input, FormStateInterface $form_state) { $return = parent::value($element, $input, $form_state); // When an element is loaded, focal_point needs to be set. During a form // submission the value will already be there. if (isset($return['target_id']) && !isset($return['focal_point'])) { /** @var \Drupal\file\FileInterface $file */ $file = \Drupal::service('entity_type.manager') ->getStorage('file') ->load($return['target_id']); $crop_type = \Drupal::config('focal_point.settings')->get('crop_type'); $crop = Crop::findCrop($file->getFileUri(), $crop_type); if ($crop) { $anchor = \Drupal::service('focal_point.manager') ->absoluteToRelative($crop->x->value, $crop->y->value, $return['width'], $return['height']); $return['focal_point'] = "{$anchor['x']},{$anchor['y']}"; } } return $return; }
/** * {@inheritdoc} */ public function massageFormValues(array $values, array $form, FormStateInterface $form_state) { // Don't do entity saving when we have validation erors. if (count($form_state->getErrors()) || !$form_state->isValidationComplete()) { return parent::massageFormValues($values, $form, $form_state); } foreach ($values as $value) { if (isset($value['fids'][0]) && isset($value['cropinfo']) && $value['cropinfo']['changed']) { $fid = $value['fids'][0]; $new_crop_info = array('fid' => $fid, 'x' => $value['cropinfo']['x'], 'y' => $value['cropinfo']['y'], 'width' => $value['cropinfo']['width'], 'height' => $value['cropinfo']['height']); imagefield_crop_update_file_info($new_crop_info); $source = File::load($fid); if ($source) { image_path_flush($source->getFileUri()); } } } return parent::massageFormValues($values, $form, $form_state); }
/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $field_settings = $this->getFieldSettings(); // Add properties needed by process() method. $element['#crop_list'] = $this->getSetting('crop_list'); $element['#crop_preview_image_style'] = $this->getSetting('crop_preview_image_style'); // Set an custom upload_location. $element['#upload_location'] = 'public://crop/pictures/'; return parent::formElement($items, $delta, $element, $form, $form_state); }