Problem/Motivation

The current implementation requires a separate computed field with a list of relationships for all blocks.

Proposed resolution

I have implemented more simplified logic that works well without a separate field and works well with reusable blocks too.
Also, it provides support for including fields from the "default_include" list, provided by jsonapi_extras module (jsonapi_defaults submodule).

Remaining tasks

User interface changes

API changes

Data model changes

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Murz created an issue. See original summary.

murz’s picture

Status: Active » Needs review

For my cases it works well, so let's move to "Needs review" to check how it works for others.

murz’s picture

Assigned: murz » Unassigned
kevinquillen’s picture

Will this recursively load info for blocks that have entity reference fields (Media, taxonomy, etc)?

kevinquillen’s picture

StatusFileSize
new22.98 KB

Here is a quick add on for this patch that does the following:

  1. Checks if $item['layout_builder__layout'] is empty.
  2. If so, load the entity type and its bundles entity view display information.
  3. If the entity view display is layout builder enabled, load the third party settings and get that information.
  4. Populate $item['layout_builder__layout'] with that information and proceed as normal - adding the entity view display entity as a cacheable dependency.

This was important in getting layout builder information for decoupled sites for nodes that are NOT overridden, or may not be able to be overridden in layout builder. Otherwise, the array is empty - but you DO have layout builder information at the display level. It should pass it.

  protected function parseResource($item, Response $response) {
    if (empty($item['layout_builder__layout'])) {
      // @todo: possible to get the "view mode" here? Also can this work for any entity type?
      [$entity_type, $bundle] = explode('--', $item['type']);
      $storage = $this->entityTypeManager->getStorage('entity_view_display');
      $entity_view_display = $storage->load($entity_type . '.' . $bundle . '.default');
      $third_party_settings = $entity_view_display->getThirdPartySettings('layout_builder');

      // @todo: Any better way of handling Section entity objects?
      if ($third_party_settings['enabled']) {
        foreach ($third_party_settings['sections'] as $section) {
          $item['layout_builder__layout'][] = $section->toArray();
        }

        $response->addCacheableDependency($entity_view_display);
      }
    }

    if (isset($item['layout_builder__layout'])) {
kevinquillen’s picture

StatusFileSize
new23.55 KB

Attaching a second patch. This one will sort the blocks in their proper order so it is easier to work with on the client side.

  /**
   * Sort blocks within sections by their weights so the order is correct in JSON:API.
   *
   * @param array $section_data
   *   A set of layouts and components within a section.
   *
   * @return array
   *   The sorted components.
   */
  protected function sortSectionComponents(array &$section_data): array {
    foreach ($section_data as &$section_components) {
      usort($section_components['components'], fn($a, $b) => $a['weight'] <=> $b['weight']);
    }

    return $section_data;
  }
kevinquillen’s picture

StatusFileSize
new24.6 KB

I noticed that core does not store the order of items correctly, region or weight. This updated patch attempts to set that order while building the response so the frontend does not have to bear that burden.

kevinquillen’s picture

StatusFileSize
new25.6 KB

Updating patch. Reworked some of the logic and addressed issues when viewing entities that are not Layout Builder enabled.

quadrexdev’s picture

StatusFileSize
new25.62 KB

We used the patch from #9 in our project and it worked fine except for supporting the includes tree.

Our case:

Node with layout builder, inside layout builder layout added some blocks with paragraphs (just a basic block type with a paragraph field) -> paragraphs were not included even after configuring default includes.

What helped:

Adding "include" parameter before

    $this->rootParser->parse($response);

Attaching patch file

kevinquillen’s picture

That could have been overlooked, sure - I don't use Layout Paragraphs or Paragraphs.

the_g_bomb’s picture

I grabbed this patch along with the one for D11 readiness, both applied nicely, and I am now getting my node information for layout builder for nodes just using the default layout. I can't speak for paragraphs as I don't have those. If I get time, I will try to test further. Looks good to me, though. Thank you