You are here

function panopoly_magic_ajax_update_preview in Panopoly Magic 7

Ajax callback that just returns the rendered preview.

1 string reference to 'panopoly_magic_ajax_update_preview'
panopoly_magic_form_alter in ./panopoly_magic.module
Implements hook_form_alter()

File

./panopoly_magic.module, line 683

Code

function panopoly_magic_ajax_update_preview($form, $form_state) {
  if (isset($form_state['values']) && isset($form['#panopoly_magic_preview_info']['configuration'])) {
    $form['#panopoly_magic_preview_info']['configuration'] = $form_state['values'] + $form['#panopoly_magic_preview_info']['configuration'];

    // For some reason, the formatter options aren't in $form_state['values']
    // but only $form_state['input']. Copy those explicitly.
    if ($form['#form_id'] == 'ctools_entity_field_content_type_formatter_options') {
      foreach ($form['#panopoly_magic_preview_info']['configuration']['formatter_settings'] as $formatter_setting_key => $formatter_setting_value) {
        $form['#panopoly_magic_preview_info']['configuration']['formatter_settings'][$formatter_setting_key] = isset($form_state['input'][$formatter_setting_key]) ? $form_state['input'][$formatter_setting_key] : $formatter_setting_value;
      }
    }
  }

  // If this is a field is being editted via FAPE, then we need to update the
  // entity in context with the temporary value of the field, in order for it
  // to be rendered correctly in the preview.
  if (isset($form_state['values']) && $form_state['plugin']['name'] == 'entity_field' && isset($form_state['subform_id']) && $form_state['subform_id'] == 'fape_field_edit_field_form' && isset($form['#panopoly_magic_preview_info']['context']['panelizer'])) {
    $entity_type = $form['#panopoly_magic_preview_info']['context']['panelizer']->type[2];
    $entity =& $form['#panopoly_magic_preview_info']['context']['panelizer']->data;
    foreach ($form_state['field'] as $field_name => $lang_field) {
      field_attach_form_validate($entity_type, $entity, $form, $form_state, array(
        'field_name' => $field_name,
      ));
      field_attach_submit($entity_type, $entity, $form, $form_state, array(
        'field_name' => $field_name,
      ));

      // Run the hook_field_load() before rendering. This is necessary for some
      // fields, like file and image fields.
      list($entity_id, , ) = entity_extract_ids($entity_type, $entity);
      $a = FIELD_LOAD_CURRENT;
      $b = NULL;
      _field_invoke_multiple('load', $entity_type, array(
        $entity_id => $entity,
      ), $a, $b, array(
        'field_name' => $field_name,
      ));
    }
  }

  // If this is a preview for a View, and it's using any caching, then clear
  // the cache before rendering the View.
  if (isset($form_state['view']) && get_class($form_state['view']) == 'view') {
    $view = $form_state['view'];
    if ($cache = $view->display_handler
      ->get_plugin('cache')) {
      $cache
        ->cache_flush();
    }
  }
  return panopoly_magic_form_post_render_preview('', $form);
}