示例#1
0
 /**
  * {@inheritdoc}
  */
 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
 {
     $element = parent::formElement($items, $delta, $element, $form, $form_state);
     $field_settings = $this->getFieldSettings();
     // Add upload resolution validation.
     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']);
     }
     // 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 properties needed by process() method.
     $element['#preview_image_style'] = $this->getSetting('preview_image_style');
     $element['#title_field'] = $field_settings['title_field'];
     $element['#title_field_required'] = $field_settings['title_field_required'];
     $element['#alt_field'] = $field_settings['alt_field'];
     $element['#alt_field_required'] = $field_settings['alt_field_required'];
     // Default image.
     $default_image = $field_settings['default_image'];
     if (empty($default_image['uuid'])) {
         $default_image = $this->fieldDefinition->getFieldStorageDefinition()->getSetting('default_image');
     }
     // Convert the stored UUID into a file ID.
     if (!empty($default_image['uuid']) && ($entity = \Drupal::entityManager()->loadEntityByUuid('file', $default_image['uuid']))) {
         $default_image['fid'] = $entity->id();
     }
     $element['#default_image'] = !empty($default_image['fid']) ? $default_image : array();
     return $element;
 }
示例#2
0
 /**
  * {@inheritdoc}
  */
 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
 {
     $element = parent::formElement($items, $delta, $element, $form, $form_state);
     $field_settings = $this->getFieldSettings();
     // Add upload resolution validation.
     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']);
     }
     // 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'][] = array(get_class($this), 'process');
     // Add properties needed by process() method.
     $element['#preview_image_style'] = $this->getSetting('preview_image_style');
     $element['#title_field'] = $field_settings['title_field'];
     $element['#title_field_required'] = $field_settings['title_field_required'];
     $element['#alt_field'] = $field_settings['alt_field'];
     $element['#alt_field_required'] = $field_settings['alt_field_required'];
     return $element;
 }
 /**
  * {@inheritdoc}
  */
 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
 {
     $element = parent::formElement($items, $delta, $element, $form, $form_state);
     // If not using custom extension validation, ensure this is a video.
     $element['#upload_location'] = $this->getUploadLocation();
     $element['#upload_validators'] = $this->getUploadValidators();
     return $element;
 }
 /**
  * {@inheritdoc}
  */
 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
 {
     if (empty($items[$delta]->getValue())) {
         $element['value'] = $element + array('#type' => 'textfield', '#attributes' => ['class' => ['js-text-full', 'text-full']], '#element_validate' => [[get_class($this), 'validateFormElement']], '#allowed_providers' => $this->getSetting('allowed_providers'));
     } else {
         $element += parent::formElement($items, $delta, $element, $form, $form_state);
     }
     return $element;
 }