core/lib/Drupal/Component/Utility/DiffArray.php | 34 +++++++++++++++++++++++++ core/modules/editor/editor.module | 3 ++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/core/lib/Drupal/Component/Utility/DiffArray.php b/core/lib/Drupal/Component/Utility/DiffArray.php index 825648e..e5bc679 100644 --- a/core/lib/Drupal/Component/Utility/DiffArray.php +++ b/core/lib/Drupal/Component/Utility/DiffArray.php @@ -47,4 +47,38 @@ public static function diffAssocRecursive(array $array1, array $array2) { return $difference; } + /** + * Computes the difference of arrays. + * + * The main difference from the array_diff() is that this method does not + * remove duplicates. For example: + * @code + * array_diff([1, 1, 1], [1]); // [] + * \Drupal\Component\Utility\DiffArray::diffOnce([1, 1, 1], [1]); // [1, 1] + * @endcode + * + * Keys are maintained from the $array1. + * + * The comparison of items is always performed in the strict (===) mode. + * + * @param array $array1 + * The array to compare from. + * @param array $array2 + * The array to compare to. + * + * @return array + * Returns the difference between the two arrays. + */ + public static function diffOnce(array $array1, array $array2) { + foreach ($array2 as $item) { + // Always use strict mode because otherwise there could be fatal errors on + // object conversions. + $key = array_search($item, $array1, TRUE); + if ($key !== FALSE) { + unset($array1[$key]); + } + } + return $array1; + } + } diff --git a/core/modules/editor/editor.module b/core/modules/editor/editor.module index 7cba938..f181ef1 100644 --- a/core/modules/editor/editor.module +++ b/core/modules/editor/editor.module @@ -5,6 +5,7 @@ * Adds bindings for client-side "text editors" to text formats. */ +use Drupal\Component\Utility\DiffArray; use Drupal\Component\Utility\Html; use Drupal\editor\Entity\Editor; use Drupal\Core\Entity\FieldableEntityInterface; @@ -386,7 +387,7 @@ function editor_entity_update(EntityInterface $entity) { // Detect file usages that should be incremented. foreach ($uuids_by_field as $field => $uuids) { - $added_files = array_diff($uuids_by_field[$field], $original_uuids_by_field[$field]); + $added_files = DiffArray::diffOnce($uuids_by_field[$field], $original_uuids_by_field[$field]); _editor_record_file_usage($added_files, $entity); }