You are here

function panelizer_quickedit_render_field in Quick Edit 7

Implements hook_quickedit_render_field().

See also

_panelizer_generate_quickedit_viewmode()

File

includes/panelizer.inc, line 103
Implements Quick Edit module hooks on behalf of panelizer.module.

Code

function panelizer_quickedit_render_field($entity_type, $entity, $field_name, $view_mode_id, $langcode) {
  ctools_include('plugins', 'panels');
  ctools_include('content', 'ctools');
  list($module, $panelizer_view_mode) = explode('-', $view_mode_id);

  // Now render the given field (which resides in the given pane ID) through
  // Panels' render pipeline.
  $args = array(
    entity_id($entity_type, $entity),
  );

  // @see panelizer_panelizer_task_render()
  $entity_handler = panelizer_entity_plugin_get_handler($entity_type);

  // @see PanelizerEntityDefault.class.php::render_entity().
  $panelizer = $entity->panelizer[$panelizer_view_mode];

  // Special case: the title.
  // Note: it is technically impossible to determine what should be rendered
  // exactly, because it is determined by a subset of a template. Hence we do a
  // best-effort approximation.
  // @see panelizer-view-mode.tpl.php
  if ($field_name === 'title') {
    $entity_id = entity_id($entity_type, $entity);

    // @see PanelizerEntityDefault::preprocess_panelizer_view_mode()
    $title_element = 'h2';
    if (!empty($panelizer->title_element)) {
      $title_element = $panelizer->title_element;
    }
    $entity_url = NULL;
    if (!empty($panelizer->link_to_entity)) {
      $bits = explode('/', $entity_handler->plugin['entity path']);
      foreach ($bits as $count => $bit) {
        if (strpos($bit, '%') === 0) {
          $bits[$count] = $entity_id;
        }
      }
      $entity_url = url(implode('/', $bits));
    }

    // Immediately return the rendered title pseudo-field.
    $quickedit_id = "{$entity_type}/{$entity_id}/title/{$langcode}/{$view_mode_id}";
    $prefix = '<' . $title_element . ' data-quickedit-field-id="' . $quickedit_id . '">';
    $suffix = '</' . $title_element . '>';
    if (isset($entity_url)) {
      $markup = $prefix . '<a href="' . $entity_url . '">' . $entity->title . '</a>' . $suffix;
    }
    else {
      $markup = $prefix . $entity->title . $suffix;
    }
    return array(
      '#markup' => $markup,
    );
  }

  // @see PanelizerEntityDefault.class.php::render_entity().
  $display = $panelizer->display;
  $display->context = $entity_handler
    ->get_contexts($panelizer, $entity);
  $display->args = $args;
  $display->css_id = $panelizer->css_id;
  $renderer = panels_get_renderer_handler($panelizer->pipeline, $display);

  // Find the ID of the pane where the given field resides; it's impossible to
  // know which pane ID the field is being rendered in when the
  // data-quickedit-field-id attribute gets set in quickedit_preprocess_field(),
  // so sadly we have to resort to this work-around. This *will* break down if
  // the same field is rendered multiple times.
  $pane_id = NULL;
  foreach ($display->content as $id => $pane) {
    if ($pane->type === 'entity_field' && $pane->subtype === $entity_type . ':' . $field_name) {
      $pane_id = $id;
      break;
    }
  }

  // @see panels_renderer_standard::render_panes()
  $pane = $display->content[$pane_id];

  // @see panels_renderer_standard::render_pane()
  module_invoke_all('panels_pane_prerender', $pane);
  $pane_content = $renderer
    ->render_pane_content($pane);

  // Finally, return the renderable array containing the field.
  return $pane_content->content;
}