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 index 6c95619..55bef1f 100644 --- 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 @@ -27,6 +27,9 @@ public function update() { $this->updateFileUsage(); } + /** + * Updates the file usage. + */ protected function updateFileUsage() { $entity = $this->getRoot(); @@ -39,35 +42,29 @@ protected function updateFileUsage() { return; } - // Get the field id. - $field_id = $this->getInstance()->getField()->id(); - // Build a list of the current target IDs. - $current_fids = array(); + $fids = array(); foreach ($this->list as $item) { - $current_fids[] = $item->target_id; + $fids[] = $item->target_id; } // Compare the original field values with the ones that are being saved. - $original = $entity->original->getBCEntity(); - $langcode = $original->language()->id; - + $field_name = $this->getFieldDefinition()->getFieldName(); $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)) { + if (!empty($entity->original->$field_name)) { + foreach ($entity->original->$field_name as $item) { + $original_fids[] = $item->target_id; + if (isset($item->target_id) && !in_array($item->target_id, $fids)) { // Decrement the file usage count by 1. - file_usage()->delete(file_load($original_item['target_id']), 'file', $entity->entityType(), $entity->id()); + file_usage()->delete($item->entity, '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()); + if (!in_array($item->target_id, $original_fids)) { + file_usage()->add($item->entity, '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 1d9ce13..5de9a76 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 @@ -100,23 +100,20 @@ public static function schema(FieldInterface $field) { */ public function getPropertyDefinitions() { $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'] : ''; - if (!isset(static::$propertyDefinitions[$key])) { - static::$propertyDefinitions[$key] = parent::getPropertyDefinitions(); + if (!isset(static::$propertyDefinitions)) { + static::$propertyDefinitions = parent::getPropertyDefinitions(); - static::$propertyDefinitions[$key]['display'] = array( + static::$propertyDefinitions['display'] = array( 'type' => 'boolean', 'label' => t('Flag to control whether this file should be displayed when viewing content.'), ); - static::$propertyDefinitions[$key]['description'] = array( + static::$propertyDefinitions['description'] = array( 'type' => 'string', 'label' => t('A description of the file.'), ); } - return static::$propertyDefinitions[$key]; + return static::$propertyDefinitions; } /** @@ -217,41 +214,40 @@ public function instanceSettingsForm(array $form, array &$form_state) { * {@inheritdoc} */ public function insert() { + // @todo Move in FileField in https://drupal.org/node/2073033. $entity = $this->getRoot(); - $target_id = $this->get('target_id')->getValue(); // Add a new usage for this new uploaded file. - file_usage()->add(file_load($target_id), 'file', $entity->entityType(), $entity->id()); + file_usage()->add($this->entity, 'file', $entity->entityType(), $entity->id()); } /** * {@inheritdoc} */ public function delete() { + // @todo Move in FileField in https://drupal.org/node/2073033. $entity = $this->getRoot(); - $target_id = $this->get('target_id')->getValue(); // Delete all file usages within this entity. - file_usage()->delete(file_load($target_id), 'file', $entity->entityType(), $entity->id(), 0); + file_usage()->delete($this->entity, 'file', $entity->entityType(), $entity->id(), 0); } /** * {@inheritdoc} */ public function deleteRevision() { + // @todo Move in FileField in https://drupal.org/node/2073033. $entity = $this->getRoot(); - $target_id = $this->get('target_id')->getValue(); // Decrement the file usage count by 1. - file_usage()->delete(file_load($target_id), 'file', $entity->entityType(), $entity->id()); + file_usage()->delete($this->entity, 'file', $entity->entityType(), $entity->id()); } /** * {@inheritdoc} */ public function isEmpty() { - $target_id = $this->get('target_id')->getValue(); - return empty($target_id); + return empty($this->target_id); } /** 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 f9fa936..2bb8236 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 @@ -108,31 +108,28 @@ public static function schema(FieldInterface $field) { */ public function getPropertyDefinitions() { $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'] : ''; - if (!isset(static::$propertyDefinitions[$key])) { - static::$propertyDefinitions[$key] = parent::getPropertyDefinitions(); + if (!isset(static::$propertyDefinitions)) { + static::$propertyDefinitions = parent::getPropertyDefinitions(); - static::$propertyDefinitions[$key]['alt'] = array( + static::$propertyDefinitions['alt'] = array( 'type' => 'string', 'label' => t("Alternative image text, for the image's 'alt' attribute."), ); - static::$propertyDefinitions[$key]['title'] = array( + static::$propertyDefinitions['title'] = array( 'type' => 'string', 'label' => t("Image title text, for the image's 'title' attribute."), ); - static::$propertyDefinitions[$key]['width'] = array( + static::$propertyDefinitions['width'] = array( 'type' => 'integer', 'label' => t('The width of the image in pixels.'), ); - static::$propertyDefinitions[$key]['height'] = array( + static::$propertyDefinitions['height'] = array( 'type' => 'integer', 'label' => t('The height of the image in pixels.'), ); } - return static::$propertyDefinitions[$key]; + return static::$propertyDefinitions; } /**