I have a group panel panes common for several pages. I'm trying to refactor them in a mini-panel for ease of maintenance. I run into difficulties with passing taxonomy term through context. I need an optional context so I can pass it on one panel page and skip on another. Unfortunately, contexts tab of mini-panels UI has quite a strange behavior. If I try to add an optional context I get a form which requires me to select an entity id. I guess, it's a default argument, but I don't need one. Moreover, if I select a random term and finish creating a mini-panel I can't select which context to pass to it when I add it to parent page.

If I add a required context to mini-panel, all seems to work, but I need an optional one.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

akamaus’s picture

Title: How to use mini-panles with optional contexts? » How to use mini-panеls with optional contexts? Can't pass context to panel.
stevector’s picture

I made some mini-panels context-optional on a client project a few months ago in a somewhat hard-coded fashion. I'll dig out that code and work on converting it to work generically.

diwant’s picture

@stevector Can I help you dig for it? I need this too, but I'd like to see how you did it since that'll save me some lurnin'

stevector’s picture

Title: How to use mini-panеls with optional contexts? Can't pass context to panel. » Add "optional" checkbox to require contexts within mini panels
Version: 7.x-3.2 » 7.x-3.x-dev
Category: support » feature

The alter approach I used looked like this:

/**
 * Implements hook_ctools_plugin_post_alter.
 *
 * This hook allow us to change various callbacks for plugins. Here it is used
 * to change the functions called to define mini_panel content types.
 *
 * hook_ctools_plugin_pre_alter is also available. I do not know when one would
 * be more advantageous than the other.
 */
function custom_ctools_plugin_post_alter(&$plugin, &$info) {

  if ($plugin['plugin type'] == 'content_types'    && $plugin['module'] == 'panels_mini') {
    $plugin['content type'] = 'custom_panels_mini_panels_mini_content_type_content_type';
    $plugin['content types'] = 'custom_panels_mini_panels_mini_content_type_content_types';
  }
}

/**
 * Return each available mini panel available as a subtype.
 *
 * This function serves the purpose of an alter hook on panels_mini content types.
 * This changes the required context of the header mini panel into an optional context.
 *
 * This function runs when selecting single panels_mini content types.
 */
function custom_panels_mini_panels_mini_content_type_content_type($subtype_id, $plugin) {
  $output = panels_mini_panels_mini_content_type_content_type($subtype_id, $plugin);

  // Change a required context into an optional one.
  if ($subtype_id === 'header' || $subtype_id === 'node_sidebar') {
    $output['required context'][0] = new ctools_context_optional(t('Node'), 'node');
  }

  return $output;
}

/**
 * Return each available mini panel available as a subtype.
 *
 * This function serves the purpose of an alter hook on panels_mini content types.
 * This changes the required context of the header mini panel into an optional context.
 *
 * This function runs when selecting all panels_mini content types.
 */
function custom_panels_mini_panels_mini_content_type_content_types($plugin) {
  $output = panels_mini_panels_mini_content_type_content_types($plugin);

  // Change a required context into an optional one.
  $output['header']['required context'][0]       = new ctools_context_optional(t('Node'), 'node');
  $output['node_sidebar']['required context'][0] = new ctools_context_optional(t('Node'), 'node');

  return $output;
}

I'm changing the title of this issue because this is a doable patch and there is already a TODO in the code for it: http://drupalcode.org/project/panels.git/blob/refs/heads/7.x-3.x:/panels...

I'm not sure when or if I'll have time to work on such a patch.

stevector’s picture

The patch should just be a form alter on the required context form to add an "optional" checkbox. Then a conditional check for that setting in _panels_mini_panels_mini_content_type_content_type().

And I suppose there are UI concerns around how confusing it'd be to have a "required" context be optional.

stevector’s picture

Status: Active » Needs review
FileSize
2.73 KB

Here's the patch. Perhaps the UX concern of required vs. optional could happen in a follow up issue.

diwant’s picture

Status: Needs review » Reviewed & tested by the community

It works for me, good job! I patched 7.x-3.3 and it applied and worked there.

diwant’s picture

I'll specify what I tried: I made 2 minipanels with optional contexts, and each showed the id of the contextual argument entity as content. I then placed them in a container panel. One of the 2 got fed the context and the other didn't. Worked as I would expect where they both could be added to the panel, and he first just didn't show it's content (the one that didn't get it's optional context).

merlinofchaos’s picture

Status: Reviewed & tested by the community » Needs work

The form alter is too permissive, and it will cause the [] optional checkbox to show up in places it shouldn't. For example, add a 'string' context to any panel page, and you'll see that checkbox, with mini panel specific language.

I actually think the checkbox should just be attached to all required contexts in CTools, though, so I don't really like the form alter as a patch here.

geek-merlin’s picture

Issue summary: View changes
Status: Needs work » Needs review
FileSize
1.43 KB
1.61 KB

Patch flying in, implementing merlins hint:
* one patch for ctools with the optional-checkbox only for required contexts.
ui text similar to optional-checkbox of views-content argument input.
* one patch for panels

please test so we can get this in!

Status: Needs review » Needs work

The last submitted patch, 10: ctools-1588212-10.patch, failed testing.

geek-merlin’s picture

FileSize
1.61 KB

Patch against current dev!

geek-merlin’s picture

Project: Panels » Ctools
Version: 7.x-3.x-dev »
Component: Mini panels » Code
FileSize
1.61 KB

And tell the bot!

geek-merlin’s picture

Project: Ctools » Chaos Tool Suite (ctools)
Version: » 7.x-1.x-dev
Status: Needs work » Needs review
geek-merlin’s picture

FileSize
1.61 KB
geek-merlin’s picture

Project: Chaos Tool Suite (ctools) » Panels
Version: 7.x-1.x-dev » 7.x-3.x-dev
geek-merlin’s picture

OK now that the bot gave green lights please do some manual tests.

remember: you need both:
* panels-1588212-10.patch
* ctools-1588212-11.patch

Status: Needs review » Needs work

The last submitted patch, 15: ctools-1588212-11.patch, failed testing.

axel.rutz queued 10: panels-1588212-10.patch for re-testing.

geek-merlin’s picture

It looks like the ctools patch and the panels patch should have their own issue, so created #2405187: Add "optional" checkbox for required contexts for ctools.

So ploease ignore the ctools patches here, the patch to commit is the panels patch i just requeued.

SocialNicheGuru’s picture

This works.

geek-merlin’s picture

Status: Needs work » Reviewed & tested by the community

so the green panels patch from #10 is rtbc as of #22.

sorry again for the confusion by mixing projects.

japerry’s picture

Status: Reviewed & tested by the community » Fixed

The upstream ctools patch got in, this one looks good too. Committed.

  • japerry committed d2b2969 on 7.x-3.x authored by axel.rutz
    Issue #1588212 by axel.rutz, stevector: Add "optional" checkbox to...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

geek-merlin’s picture

sylus’s picture

I created a follow up issue to this one so that Mini Panels when sometimes used with the block system (for components sometimes needed outside of Panels) will work with optional context.

#2508433: Blocks dont support recent addition of "optional" checkbox to require contexts within mini panels

misthero’s picture

scottalan’s picture

https://www.drupal.org/node/1588212#comment-8801845 patch for panels (panels-1588212-10.patch) worked for me as well. Would be great to get this in!

mpotter’s picture

Status: Needs work » Fixed

So, #10 was already commited to Panels. And to answer #27, Ctools 1.9 includes the needed patch. So I think this is Fixed and not "Needs Work"

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.