You are here

protected function MongodbMenuTreeStorage::moveChildren in MongoDB 8

Re-parents a link's children when the link itself is moved.

Parameters

array $fields: The changed menu link.

array $original: The original menu link.

Overrides MenuTreeStorage::moveChildren

File

src/MongodbMenuTreeStorage.php, line 321
Contains \Drupal\mongodb\MongodbMenuTreeStorage .

Class

MongodbMenuTreeStorage

Namespace

Drupal\mongodb

Code

protected function moveChildren($fields, $original) {
  $query = $this
    ->getChildrenQuery($original);
  $shift = $fields['depth'] - $original['depth'];
  $hex = function ($string) {
    return '\\x' . implode('\\x', str_split(bin2hex($string), 2));
  };
  $map = new \MongoCode("function () {\n      this.value['p'] = this.value['p'].replace('" . $hex($original['p']) . "', '" . $hex($fields['p']) . "');\n      this.value['menu_name'] = '" . $fields['menu_name'] . "';\n      this.value['depth'] += {$shift};\n      emit(this._id, this.value);\n    }");
  $collection = $this
    ->mongoCollection();
  $reduce = new \MongoCode("function () { }");
  $collection_name = $collection
    ->getName();
  $collection->db
    ->command([
    'mapreduce' => $collection_name,
    'map' => $map,
    'reduce' => $reduce,
    'out' => [
      'merge' => $collection_name,
    ],
    'query' => $query,
    'sort' => [
      '_id' => 1,
    ],
  ]);
}