You are here

public function GatsbyInstantPreview::bundleData in Gatsby Live Preview & Incremental Builds 2.0.x

Same name and namespace in other branches
  1. 8 modules/gatsby_instantpreview/src/GatsbyInstantPreview.php \Drupal\gatsby_instantpreview\GatsbyInstantPreview::bundleData()

Bundles entity JSON data so it can be passed in a single request.

2 calls to GatsbyInstantPreview::bundleData()
GatsbyInstantPreview::gatsbyPrepareData in modules/gatsby_instantpreview/src/GatsbyInstantPreview.php
Prepares Gatsby Data to send to the preview and build servers.
GatsbyInstantPreview::gatsbyPrepareDelete in modules/gatsby_instantpreview/src/GatsbyInstantPreview.php
Triggers the refreshing of Gatsby preview and incremental builds.

File

modules/gatsby_instantpreview/src/GatsbyInstantPreview.php, line 161

Class

GatsbyInstantPreview
Defines a class to extend the default preview system with an instant preview.

Namespace

Drupal\gatsby_instantpreview

Code

public function bundleData($key, $url, $json) {
  $updated =& self::$updateData;

  // The first time this method is called our updated data array is
  // empty so we can just return the data we were given.
  if (empty($updated)) {
    return $json;
  }
  if (!empty($updated[$key][$url]['json'])) {
    if (!empty($updated[$key][$url]['json']['data']['type'])) {

      // If there is only one entity, convert it to an array.
      $json['data'] = [
        $updated[$key][$url]['json']['data'],
        $json['data'],
      ];
    }
    else {

      // Check to make sure this entity wasn't already added.
      foreach ($updated[$key][$url]['json']['data'] as $index => $entity) {
        if ($entity['id'] == $json['id']) {

          // Update the entity data if this entity was already.
          $updated[$key][$url]['json']['data'][$index] = $json['data'];
          $json['data'] = $updated[$key][$url]['json']['data'];
          return $json;
        }
      }

      // Add new entities to the updated json data array.
      $updated[$key][$url]['json']['data'][] = $json['data'];

      // Update our json data array with the updated entities.
      $json['data'] = $updated[$key][$url]['json']['data'];
    }
  }
  return $json;
}