diff --git a/panopoly_magic.module b/panopoly_magic.module index 7f34b51..ab008f3 100644 --- a/panopoly_magic.module +++ b/panopoly_magic.module @@ -7,6 +7,15 @@ define('PANOPOLY_ADD_PREVIEW_DISABLED', 2); define('PANOPOLY_ADD_PREVIEW_DEFAULT', PANOPOLY_ADD_PREVIEW_AUTOMATIC); /** + * Implements hook_ctools_plugin_directory() + */ +function panopoly_magic_ctools_plugin_directory($owner, $plugin_type) { + if ($owner == 'ctools' && $plugin_type == 'content_types') { + return 'plugins/' . $plugin_type; + } +} + +/** * Implements hook_init() */ function panopoly_magic_init() { @@ -246,44 +255,45 @@ function panopoly_magic_form_alter(&$form, &$form_state, $form_id) { $clicked_button = empty($form_state['triggering_element']['#value']) ? '' : $form_state['triggering_element']['#value']; if ($form_id == 'fieldable_panels_panes_fieldable_panels_pane_content_type_edit_form' && ($clicked_button == 'Update Preview' || ($form_state['op'] == 'add' && empty($form_state['input']['form_build_id'])))) { - // Determine the current VID for the element - if (!empty($form_state['entity']->fpid)) { - $current_vid = db_query("SELECT vid FROM {fieldable_panels_panes} WHERE fpid = :fpid", array(':fpid' => $form_state['entity']->fpid))->fetchField(); - } - // Run entity information through standard submission form if this is a new or updated object // Update the pane settngs with the VID afterwards if (!$form_state['entity']->reusable || $form_state['op'] != 'add' || $form_state['rebuild'] == TRUE) { - // Process the entity to create a preview. Set the FPID to be the correct version. We need to - // generate a fake set of values to avoid notices from FPP as per http://drupal.org/node/1791734. - $default_values = array( - 'language' => '', - 'title' => '', - 'link' => '', - 'path' => '', - 'reusable' => '', - 'category' => '', - 'admin_title' => '', - 'admin_description' => '', - 'revision' => '', - ); - - if (!empty($form_state['values'])) { - $form_state['values'] = array_merge($default_values, $form_state['values']); - } - else { - $form_state['values'] = $default_values; + // Process the entity to create a preview. + + // + // This code is copied from fieldable_panels_panes_entity_edit_form_submit(): + // + + $entity = $form_state['entity']; + + // Copy hardcoded fields. + $entity->title = $form_state['values']['title']; + $entity->link = $form_state['values']['link']; + $entity->path = $form_state['values']['path']; + $entity->language = $form_state['values']['language']; + $entity->reusable = $form_state['values']['reusable']; + $entity->category = $form_state['values']['category']; + $entity->admin_title = $form_state['values']['admin_title']; + $entity->admin_description = $form_state['values']['admin_description']; + + $entity->revision = $form_state['values']['revision']; + // Only set a log message if there was a new revision. This prevents + // overwriting a log message on the current revision + if ($entity->revision) { + $entity->log = $form_state['values']['log']; } + field_attach_submit('fieldable_panels_pane', $entity, $form, $form_state); - fieldable_panels_panes_entity_edit_form_submit($form, $form_state); - $preview_subtype = 'vid:' . $form_state['entity']->vid; - $pane->subtype = 'fpid:' . $form_state['entity']->fpid; - } + // + // End code copied from fieldable_panels_panes_entity_edit_form_submit(). + // - // Properly adjust the VID if we are dealing with an existing object - if (!empty($current_vid)) { - db_query("UPDATE {fieldable_panels_panes} SET vid = :vid WHERE fpid = :fpid", array(':vid' => $current_vid, ':fpid' => $form_state['entity']->fpid)); + // Define the content type plugin to use for rendering the preview. + $preview_type = 'fieldable_panels_pane_preview'; + + // Stash the entity stub for the preview content type. + $pane->configuration['fieldable_panels_pane'] = $entity; } } @@ -331,7 +341,7 @@ function panopoly_magic_form_alter(&$form, &$form_state, $form_id) { // inside form and double rendering. $form['#post_render'][] = 'panopoly_magic_form_post_render_preview'; $form['#panopoly_magic_preview_info'] = array( - 'preview_subtype' => isset($preview_subtype) ? $preview_subtype : NULL, + 'preview_type' => isset($preview_type) ? $preview_type : NULL, 'pane' => $pane, 'configuration' => $configuration, 'keywords' => $keywords, @@ -470,7 +480,16 @@ function panopoly_magic_ajax_update_preview($form, $form_state) { */ function panopoly_magic_form_post_render_preview($output, $form) { extract($form['#panopoly_magic_preview_info']); - $content = (empty($preview_subtype)) ? ctools_content_render($pane->type, $pane->subtype, $configuration, $keywords, $args, $context) : ctools_content_render($pane->type, $preview_subtype, $configuration, $keywords, $args, $context); + + // If no preview type was specified, render the pane as normal. + if (empty($preview_type)) { + $content = ctools_content_render($pane->type, $pane->subtype, $configuration, $keywords, $args, $context); + } + // If a preview type was specified, use that to render the preview. + else { + $content = ctools_content_render($preview_type, $pane->subtype, $configuration, $keywords, $args, $context); + } + // Create the fieldset with appropriate content $preview = array( diff --git a/plugins/content_types/fieldable_panels_pane_preview.inc b/plugins/content_types/fieldable_panels_pane_preview.inc new file mode 100644 index 0000000..a6e8ac5 --- /dev/null +++ b/plugins/content_types/fieldable_panels_pane_preview.inc @@ -0,0 +1,43 @@ + t('Fielded custom content (preview)'), + 'no title override' => TRUE, + 'description' => t('Preview for created custom panels pane with fields'), + 'category' => t('Fielded panes'), + 'all contexts' => TRUE, + 'defaults' => array( + 'view_mode' => 'full', + ), +); + +/** + * Callback to render our content type preview. + * + * Render FPP based on entity stashed in configuration for the preview. + * + * @see fieldable_panels_panes_fieldable_panels_pane_content_type_render(). + */ +function panopoly_magic_fieldable_panels_pane_preview_content_type_render($subtype, $conf, $panel_args = array(), $context = array()) { + $entity = $conf['fieldable_panels_pane']; + $view_mode = isset($conf['view_mode']) ? $conf['view_mode'] : 'full'; + if ($entity && fieldable_panels_panes_access('view', $entity)) { + $settings = field_bundle_settings('fieldable_panels_pane', $entity->bundle); + $block = new stdClass(); + $block->title = ''; + if (empty($settings['extra_fields']['display']) || !empty($settings['extra_fields']['display']['title']['default']['visible'])) { + $block->title = !empty($entity->title) ? filter_xss_admin($entity->title) : ''; + } + $block->content = fieldable_panels_panes_view($entity, $view_mode); + return $block; + } +}