You are here

function panelizer_quickedit_render_field in Panelizer 8.4

Same name and namespace in other branches
  1. 8.5 panelizer_quickedit/panelizer_quickedit.module \panelizer_quickedit_render_field()
  2. 8.3 panelizer_quickedit/panelizer_quickedit.module \panelizer_quickedit_render_field()

Implements hook_quickedit_render_field().

File

panelizer_quickedit/panelizer_quickedit.module, line 10

Code

function panelizer_quickedit_render_field(EntityInterface $entity, $field_name, $view_mode_id, $langcode) {

  // Ensure that the correct translation of this Entity is loaded.
  $entity = \Drupal::service('entity.repository')
    ->getTranslationFromContext($entity, $langcode);

  // Grab the information required to re-render the entity_field block.
  $temp = str_replace('panelizer-', '', $view_mode_id);
  list($view_mode, $block_id) = explode('-block-id-', $temp);

  // Load the Panelizer display.

  /** @var \Drupal\panelizer\PanelizerInterface $panelizer */
  $panelizer = \Drupal::service('panelizer');
  $display = $panelizer
    ->getPanelsDisplay($entity, $view_mode);

  /** @var \Drupal\ctools_block\Plugin\Block\EntityField $plugin */
  $plugin = $display
    ->getBlock($block_id);

  // Set the appropriate Entity context and build the plugin.
  $plugin
    ->setContextValue('entity', $entity);
  $build = $plugin
    ->build();

  // Add our custom field view-mode in case the user wants to edit again.
  $build['field']['#view_mode'] = $view_mode_id;
  return $build;
}