diff --git a/core/modules/file/lib/Drupal/file/FileField.php b/core/modules/file/lib/Drupal/file/FileField.php deleted file mode 100644 index b5aaeb8..0000000 --- a/core/modules/file/lib/Drupal/file/FileField.php +++ /dev/null @@ -1,72 +0,0 @@ -updateFileUsage(); - } - - protected function updateFileUsage() { - $entity = $this->getRoot(); - - // On new revisions, all files are considered to be a new usage and no - // deletion of previous file usages are necessary. - if (!empty($entity->original) && $entity->getRevisionId() != $entity->original->getRevisionId()) { - foreach ($this->list as $item) { - $target_id = $item->get('target_id')->getValue(); - file_usage()->add(file_load($target_id), 'file', $entity->entityType(), $entity->id()); - } - return; - } - - // Get the field id. - $field_id = $this->getInstance()->getField()->id(); - - // Build a list of the current target IDs. - $current_fids = array(); - foreach ($this->list as $item) { - $target_id = $item->get('target_id')->getValue(); - $current_fids[] = $target_id; - } - - // Compare the original field values with the ones that are being saved. - $original = $entity->original->getBCEntity(); - $langcode = $original->language()->id; - - $original_fids = array(); - if (!empty($original->{$field_id}[$langcode])) { - foreach ($original->{$field_id}[$langcode] as $original_item) { - $original_fids[] = $original_item['target_id']; - if (isset($original_item['target_id']) && !in_array($original_item['target_id'], $current_fids)) { - // Decrement the file usage count by 1. - file_usage()->delete(file_load($original_item['target_id']), 'file', $entity->entityType(), $entity->id()); - } - } - } - - // Add new usage entries for newly added files. - foreach ($this->list as $item) { - $target_id = $item->get('target_id')->getValue(); - if (!in_array($target_id, $original_fids)) { - file_usage()->add(file_load($target_id), 'file', $entity->entityType(), $entity->id()); - } - } - } - -} diff --git a/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileField.php b/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileField.php new file mode 100644 index 0000000..273ddb7 --- /dev/null +++ b/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileField.php @@ -0,0 +1,70 @@ +updateFileUsage(); + } + + protected function updateFileUsage() { + $entity = $this->getRoot(); + + // On new revisions, all files are considered to be a new usage and no + // deletion of previous file usages are necessary. + if (!empty($entity->original) && $entity->getRevisionId() != $entity->original->getRevisionId()) { + foreach ($this->list as $item) { + file_usage()->add($item->entity, 'file', $entity->entityType(), $entity->id()); + } + return; + } + + // Get the field id. + $field_id = $this->getInstance()->getField()->id(); + + // Build a list of the current target IDs. + $current_fids = array(); + foreach ($this->list as $item) { + $current_fids[] = $item->target_id; + } + + // Compare the original field values with the ones that are being saved. + $original = $entity->original->getBCEntity(); + $langcode = $original->language()->id; + + $original_fids = array(); + if (!empty($original->{$field_id}[$langcode])) { + foreach ($original->{$field_id}[$langcode] as $original_item) { + $original_fids[] = $original_item['target_id']; + if (isset($original_item['target_id']) && !in_array($original_item['target_id'], $current_fids)) { + // Decrement the file usage count by 1. + file_usage()->delete(file_load($original_item['target_id']), 'file', $entity->entityType(), $entity->id()); + } + } + } + + // Add new usage entries for newly added files. + foreach ($this->list as $item) { + $target_id = $item->get('target_id')->getValue(); + if (!in_array($target_id, $original_fids)) { + file_usage()->add(file_load($target_id), 'file', $entity->entityType(), $entity->id()); + } + } + } + +} diff --git a/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileItem.php b/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileItem.php index 8eb6048..1d9ce13 100644 --- a/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileItem.php +++ b/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileItem.php @@ -18,7 +18,6 @@ * * @FieldType( * id = "file", - * module = "file", * label = @Translation("File"), * description = @Translation("This field stores the ID of a file as an integer value."), * settings = { @@ -34,7 +33,7 @@ * }, * default_widget = "file_generic", * default_formatter = "file_default", - * list_class = "\Drupal\file\FileField" + * list_class = "\Drupal\file\Plugin\field\field_type\FileField" * ) */ class FileItem extends EntityReferenceItem implements ConfigFieldItemInterface { @@ -174,7 +173,7 @@ public function instanceSettingsForm(array $form, array &$form_state) { '#title' => t('File directory'), '#default_value' => $settings['file_directory'], '#description' => t('Optional subdirectory within the upload destination where files will be stored. Do not include preceding or trailing slashes.'), - '#element_validate' => array(array($this, 'validateDirectory')), + '#element_validate' => array(array(get_class($this), 'validateDirectory')), '#weight' => 3, ); @@ -185,7 +184,7 @@ public function instanceSettingsForm(array $form, array &$form_state) { '#title' => t('Allowed file extensions'), '#default_value' => $extensions, '#description' => t('Separate extensions with a space or comma and do not include the leading dot.'), - '#element_validate' => array(array($this, 'validateExtensions')), + '#element_validate' => array(array(get_class($this), 'validateExtensions')), '#weight' => 1, // By making this field required, we prevent a potential security issue // that would allow files of any type to be uploaded. @@ -198,7 +197,7 @@ public function instanceSettingsForm(array $form, array &$form_state) { '#default_value' => $settings['max_filesize'], '#description' => t('Enter a value like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes) in order to restrict the allowed file size. If left empty the file sizes will be limited only by PHP\'s maximum post and file upload sizes (current limit %limit).', array('%limit' => format_size(file_upload_max_size()))), '#size' => 10, - '#element_validate' => array(array($this, 'validateMaxFilesize')), + '#element_validate' => array(array(get_class($this), 'validateMaxFilesize')), '#weight' => 5, ); @@ -256,7 +255,7 @@ public function isEmpty() { } /** - * Render API callback: Validates the file destination field. + * Form API callback * * Removes slashes from the beginning and end of the destination value and * ensures that the file directory path is not included at the beginning of the @@ -265,14 +264,14 @@ public function isEmpty() { * This function is assigned as an #element_validate callback in * instanceSettingsForm(). */ - public function validateDirectory($element, &$form_state) { + public static function validateDirectory($element, &$form_state) { // Strip slashes from the beginning and end of $element['file_directory']. $value = trim($element['#value'], '\\/'); form_set_value($element, $value, $form_state); } /** - * Render API callback: Validates the allowed file extensions field. + * Form API callback. * * This function is assigned as an #element_validate callback in * instanceSettingsForm(). @@ -281,7 +280,7 @@ public function validateDirectory($element, &$form_state) { * Commas are allowed by the end-user, but ultimately the value will be stored * as a space-separated list for compatibility with file_validate_extensions(). */ - public function validateExtensions($element, &$form_state) { + public static function validateExtensions($element, &$form_state) { if (!empty($element['#value'])) { $extensions = preg_replace('/([, ]+\.?)/', ' ', trim(strtolower($element['#value']))); $extensions = array_filter(explode(' ', $extensions)); @@ -296,15 +295,15 @@ public function validateExtensions($element, &$form_state) { } /** - * Render API callback: Validates the maximum upload size field. + * Form API callback. * * Ensures that a size has been entered and that it can be parsed by * parse_size(). * * This function is assigned as an #element_validate callback in - * finstanceSettingsForm(). + * instanceSettingsForm(). */ - public function validateMaxFilesize($element, &$form_state) { + public static function validateMaxFilesize($element, &$form_state) { if (!empty($element['#value']) && !is_numeric(parse_size($element['#value']))) { form_error($element, t('The "!name" option must contain a valid value. You may either leave the text field empty or enter a string like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes).', array('!name' => t($element['title'])))); } @@ -321,7 +320,7 @@ public function validateMaxFilesize($element, &$form_state) { * * @see token_replace() */ - public function getUri($data = array()) { + public function getUploadLocation($data = array()) { $settings = $this->getFieldSettings(); $destination = trim($settings['file_directory'], '/'); diff --git a/core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php b/core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php index 3274701..97e677c 100644 --- a/core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php +++ b/core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php @@ -198,7 +198,7 @@ public function formElement(FieldInterface $items, $delta, array $element, $lang $element_info = element_info('managed_file'); $element += array( '#type' => 'managed_file', - '#upload_location' => $items[$delta]->getUri(), + '#upload_location' => $items[$delta]->getUploadLocation(), '#upload_validators' => $items[$delta]->getUploadValidators(), '#value_callback' => 'file_field_widget_value', '#process' => array_merge($element_info['#process'], array('file_field_widget_process')), diff --git a/core/modules/image/lib/Drupal/image/Plugin/field/field_type/ImageItem.php b/core/modules/image/lib/Drupal/image/Plugin/field/field_type/ImageItem.php index 8cec182..f9fa936 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/field/field_type/ImageItem.php +++ b/core/modules/image/lib/Drupal/image/Plugin/field/field_type/ImageItem.php @@ -17,7 +17,6 @@ * * @FieldType( * id = "image", - * module = "image", * label = @Translation("Image"), * description = @Translation("This field stores the ID of an image file as an integer value."), * settings = { @@ -52,7 +51,7 @@ * }, * default_widget = "image_image", * default_formatter = "image", - * list_class = "\Drupal\file\FileField" + * list_class = "\Drupal\file\Plugin\field\field_type\FileField" * ) */ class ImageItem extends FileItem { @@ -64,7 +63,7 @@ public static function schema(FieldInterface $field) { return array( 'columns' => array( 'target_id' => array( - 'description' => 'The ID of the target entity.', + 'description' => 'The ID of the file entity.', 'type' => 'int', 'not null' => TRUE, 'unsigned' => TRUE, @@ -108,7 +107,7 @@ public static function schema(FieldInterface $field) { * {@inheritdoc} */ public function getPropertyDefinitions() { - $this->definition['settings']['target_type'] = 'image'; + $this->definition['settings']['target_type'] = 'file'; // Definitions vary by entity type and bundle, so key them accordingly. $key = $this->definition['settings']['target_type'] . ':'; $key .= isset($this->definition['settings']['target_bundle']) ? $this->definition['settings']['target_bundle'] : ''; @@ -141,6 +140,10 @@ public function getPropertyDefinitions() { */ public function settingsForm(array $form, array &$form_state, $has_data) { $element = array(); + + // We need the field-level 'default_image' setting, and $this->getSettings() + // will only provide the instance-level one, so we need to explicitly fetch + // the field. $settings = $this->getFieldDefinition()->getField()->getFieldSettings(); $scheme_options = array(); @@ -155,9 +158,6 @@ public function settingsForm(array $form, array &$form_state, $has_data) { '#description' => t('Select where the final files should be stored. Private file storage has significantly more overhead than public files, but allows restricted access to files within this field.'), ); - // When the user sets the scheme on the UI, even for the first time, it's - // updating a field because fields are created on the "Manage fields" - // page. $element['default_image'] = array( '#title' => t('Default image'), '#type' => 'managed_file', @@ -183,7 +183,7 @@ public function instanceSettingsForm(array $form, array &$form_state) { $element['max_resolution'] = array( '#type' => 'item', '#title' => t('Maximum image resolution'), - '#element_validate' => array(array($this, 'validateResolution')), + '#element_validate' => array(array(get_class($this), 'validateResolution')), '#weight' => 4.1, '#field_prefix' => '
', '#field_suffix' => '
', @@ -210,7 +210,7 @@ public function instanceSettingsForm(array $form, array &$form_state) { $element['min_resolution'] = array( '#type' => 'item', '#title' => t('Minimum image resolution'), - '#element_validate' => array(array($this, 'validateResolution')), + '#element_validate' => array(array(get_class($this), 'validateResolution')), '#weight' => 4.2, '#field_prefix' => '
', '#field_suffix' => '
', @@ -290,14 +290,12 @@ public function instanceSettingsForm(array $form, array &$form_state) { * {@inheritdoc} */ public function preSave() { - $entity = $this->getRoot(); - $target_id = $this->get('target_id')->getValue(); $width = $this->get('width')->getValue(); $height = $this->get('height')->getValue(); // Determine the dimensions if necessary. if (empty($width) || empty($height)) { - $image = \Drupal::service('image.factory')->get(file_load($target_id)->getFileUri()); + $image = \Drupal::service('image.factory')->get($this->entity->getFileUri()); if ($image->getExtension()) { $this->set('width', $image->getWidth()); @@ -309,7 +307,7 @@ public function preSave() { /** * Element validate function for resolution fields. */ - public function validateResolution($element, &$form_state) { + public static function validateResolution($element, &$form_state) { if (!empty($element['x']['#value']) || !empty($element['y']['#value'])) { foreach (array('x', 'y') as $dimension) { if (!$element[$dimension]['#value']) {