From 610e669fbfaa81ec4ad8b529f1eb3967bbbeced4 Mon Sep 17 00:00:00 2001 From: Capi Etheriel Date: Fri, 14 Feb 2014 11:46:52 -0200 Subject: [PATCH] Issue #2018329 by mikeker, barraponto: allow context substitutions in fieldable panels panes --- fieldable_panels_panes.install | 18 ++++++++++ fieldable_panels_panes.module | 45 +++++++++++++++++++++++++ plugins/content_types/fieldable_panels_pane.inc | 16 ++++++++- 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/fieldable_panels_panes.install b/fieldable_panels_panes.install index f17d3fe..54948d5 100644 --- a/fieldable_panels_panes.install +++ b/fieldable_panels_panes.install @@ -46,6 +46,11 @@ function fieldable_panels_panes_schema() { 'type' => 'varchar', 'length' => 255, ), + 'substitute' => array( + 'description' => 'Whether or not to perform keyword substitutions.', + 'type' => 'int', + 'size' => 'tiny', + ), 'reusable' => array( 'description' => 'Whether or not this entity will appear in the Add Content dialog.', 'type' => 'int', @@ -248,3 +253,16 @@ function fieldable_panels_panes_update_7107() { db_create_table('cache_entity_fieldable_panels_pane', $schema); } } + +/** + * Add context keyword substitution support. + */ +function fieldable_panels_panes_update_7108() { + if (!db_field_exists('fieldable_panels_panes', 'substitute')) { + db_add_field('fieldable_panels_panes', 'substitute', array( + 'description' => 'Whether or not to perform keyword substitutions.', + 'type' => 'int', + 'size' => 'tiny', + )); + } +} diff --git a/fieldable_panels_panes.module b/fieldable_panels_panes.module index e666152..78d3c8c 100644 --- a/fieldable_panels_panes.module +++ b/fieldable_panels_panes.module @@ -937,6 +937,45 @@ function fieldable_panels_panes_entity_edit_form($form, &$form_state) { '#id' => 'edit-reusable', ); + if (!empty($form_state['contexts'])) { + // Set extended description if both Field and Token modules are enabled, + // notifying of unlisted keywords + if (module_exists('field') && module_exists('token')) { + $description = t('If checked, context keywords will be substituted in this content. Note that custom fields may be used as keywords using patterns like %node:field_name-formatted.'); + } + elseif (!module_exists('token')) { + $description = t('If checked, context keywords will be substituted in this content. More keywords will be available if you install the Token module, see http://drupal.org/project/token.'); + } + else { + $description = t('If checked, context keywords will be substituted in this content.'); + } + + $form['substitute'] = array( + '#type' => 'checkbox', + '#title' => t('Use context keywords'), + '#description' => $description, + '#default_value' => !empty($entity->substitute), + ); + $form['contexts'] = array( + '#title' => t('Substitutions'), + '#type' => 'fieldset', + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + + $rows = array(); + foreach ($form_state['contexts'] as $context) { + foreach (ctools_context_get_converters('%' . check_plain($context->keyword) . ':', $context) as $keyword => $title) { + $rows[] = array( + check_plain($keyword), + t('@identifier: @title', array('@title' => $title, '@identifier' => $context->identifier)), + ); + } + } + $header = array(t('Keyword'), t('Value')); + $form['contexts']['context'] = array('#markup' => theme('table', array('header' => $header, 'rows' => $rows))); + } + $form['reusable']['category'] = array( '#type' => 'textfield', '#title' => t('Category'), @@ -1032,6 +1071,12 @@ function fieldable_panels_panes_entity_edit_form_submit($form, &$form_state) { $entity->log = $form_state['values']['log']; } + // The substitute field is only added when an FPP is added when contexts exist + // (eg: in the Panels UI). + if (isset($form_state['values']['substitute'])) { + $entity->substitute = $form_state['values']['substitute']; + } + field_attach_submit('fieldable_panels_pane', $entity, $form, $form_state); fieldable_panels_panes_save($entity); diff --git a/plugins/content_types/fieldable_panels_pane.inc b/plugins/content_types/fieldable_panels_pane.inc index 88f2a4f..c3bbebe 100644 --- a/plugins/content_types/fieldable_panels_pane.inc +++ b/plugins/content_types/fieldable_panels_pane.inc @@ -26,6 +26,7 @@ function fieldable_panels_panes_fieldable_panels_pane_ctools_content_types() { ), 'edit form' => 'fieldable_panels_panes_fieldable_panels_pane_content_type_edit_form', 'edit text' => t('Edit'), + 'content type' => 'fieldable_panels_panes_fieldable_panels_pane_content_type_content_type', ); } // -------------------------------------------------------------------------- @@ -40,7 +41,7 @@ function fieldable_panels_panes_fieldable_panels_pane_content_type_content_type( return $types[$subtype_name]; } else { - $entity = fieldable_panels_panes_load_entity($subtype_name); + $entity = _fieldable_panels_panes_load_entity($subtype_name); if ($entity) { return _fieldable_panels_panes_custom_content_type($entity); } @@ -79,6 +80,19 @@ function fieldable_panels_panes_fieldable_panels_pane_content_type_render($subty $block->title = !empty($entity->title) ? filter_xss_admin($entity->title) : ''; } $block->content = fieldable_panels_panes_view($entity, $view_mode); + + // Handle keyword substitutions for both title and content. + if (!empty($entity->substitute)) { + foreach (element_children($block->content) as $child) { + foreach ($block->content[$child]['#items'] as $index => $info) { + if (!empty($block->content[$child][$index]['#markup'])) { + $block->content[$child][$index]['#markup'] = ctools_context_keyword_substitute($block->content[$child][$index]['#markup'], array(), $context); + } + } + } + $block->title = ctools_context_keyword_substitute($block->title, array(), $context); + } + return $block; } } -- 1.8.5.5