You are here

public function InPlaceEditorDisplayBuilder::build in Panels 8.4

Same name and namespace in other branches
  1. 8.3 panels_ipe/src/Plugin/DisplayBuilder/InPlaceEditorDisplayBuilder.php \Drupal\panels_ipe\Plugin\DisplayBuilder\InPlaceEditorDisplayBuilder::build()

Renders a Panels display.

This is the outermost method in the Panels render pipeline. It calls the inner methods, which return a content array, which is in turn passed to the theme function specified in the layout plugin.

Parameters

Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant: The Panels display variant to render.

Return value

array Render array modified by the display builder.

Overrides StandardDisplayBuilder::build

File

panels_ipe/src/Plugin/DisplayBuilder/InPlaceEditorDisplayBuilder.php, line 160

Class

InPlaceEditorDisplayBuilder
The In-place editor display builder for viewing and editing a PanelsDisplayVariant in the same place.

Namespace

Drupal\panels_ipe\Plugin\DisplayBuilder

Code

public function build(PanelsDisplayVariant $panels_display) {

  // Check to see if the current user has permissions to use the IPE.
  $has_permission = $this->account
    ->hasPermission('access panels in-place editing') && $this->panelsStorage
    ->access($panels_display
    ->getStorageType(), $panels_display
    ->getStorageId(), 'update', $this->account)
    ->isAllowed();
  if ($has_permission) {
    $has_permission = \Drupal::service('plugin.manager.ipe_access')
      ->access($panels_display);
  }

  // Attach the Panels In-place editor library based on permissions.
  if ($has_permission) {

    // This flag tracks whether or not there are unsaved changes.
    $unsaved = FALSE;
    $locked = FALSE;

    // If a temporary configuration for this variant exists, use it.
    $temp_store_key = $panels_display
      ->getTempStoreId();
    $lock_info = $this->tempStore
      ->getMetadata($temp_store_key);
    if ($lock_info) {
      if ($lock_info->owner === $this->account
        ->id()) {
        $variant_config = $this->tempStore
          ->get($temp_store_key);
        unset($variant_config['id']);
        $panels_display
          ->setConfiguration($variant_config);

        // Indicate that the user is viewing un-saved changes.
        $unsaved = TRUE;
      }
      else {
        $locked = TRUE;
      }
    }
    $build = parent::build($panels_display);
    $regions = $panels_display
      ->getRegionAssignments();
    $layout = $panels_display
      ->getLayout();
    foreach ($regions as $region => $blocks) {

      // Wrap each region with a unique class and data attribute.
      $region_name = Html::getClass("block-region-{$region}");
      $build[$region]['#prefix'] = '<div class="' . $region_name . '" data-region-name="' . $region . '">';
      $build[$region]['#suffix'] = '</div>';
      if ($blocks) {
        foreach ($blocks as $block_id => $block) {
          $build[$region][$block_id]['#attributes']['data-block-id'] = $block_id;
        }
      }
    }

    // Attach the required settings and IPE.
    $build['#attached']['library'][] = 'panels_ipe/panels_ipe';
    $build['#attached']['drupalSettings']['panels_ipe'] = $this
      ->getDrupalSettings($regions, $layout, $panels_display, $unsaved, $locked);

    // Add our custom elements to the build.
    $build['#prefix'] = '<div id="panels-ipe-content">';
    $build['#suffix'] = '</div><div id="panels-ipe-tray"></div>';
  }
  else {
    $build = parent::build($panels_display);
  }
  return $build;
}