Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

API issue

Avatar

Level 3

We have a problem adding content to collections via API. The app shows content following the order the content in collection. However, when we add new content to collection via API, content is put at the bottom instead of at the top, resulting in showing contents in a crescent order. Which is bad because users need to see fresh content first.

This does not occurs when we add content manually (Through the Web Adobe DPS). How we control this? Is there a way to add content to collection at the "top of the pile" or maybe any way to control how content is ordered (like the manual way in the web version, dragging and dropping) ?

(Our integration uses examples from demo examples of the API and even with demo examples this is exactly the same behaviour [pushing content at the bottom of collection list]).

1 Accepted Solution

Avatar

Correct answer by
Employee

Hi,

The function that controls the order of the entities within a collection is located in /class/collection.php, updateContetnElements(). By default, it is doing this:

  // iterate through the new list of entities

  foreach($this->contentElements as $contentElement) {

    // append each entity to the end of the existing list of entities

    $data[] = array(

      'href' => $contentElement

    );

  }

$data contains the existing list of entities (can be empty), and $this->contentElements have the newly added list of entities. If you wish to prepend the new list of content elements, here's one of the ways you can do that:

  // iterate through the new list of entities

  foreach($this->contentElements as $contentElement) {

    // prepend each entity to the top of the existing list of entities

    array_unshift($data, array('href' => $contentElement));

  }

View solution in original post

1 Reply

Avatar

Correct answer by
Employee

Hi,

The function that controls the order of the entities within a collection is located in /class/collection.php, updateContetnElements(). By default, it is doing this:

  // iterate through the new list of entities

  foreach($this->contentElements as $contentElement) {

    // append each entity to the end of the existing list of entities

    $data[] = array(

      'href' => $contentElement

    );

  }

$data contains the existing list of entities (can be empty), and $this->contentElements have the newly added list of entities. If you wish to prepend the new list of content elements, here's one of the ways you can do that:

  // iterate through the new list of entities

  foreach($this->contentElements as $contentElement) {

    // prepend each entity to the top of the existing list of entities

    array_unshift($data, array('href' => $contentElement));

  }