I am trying to use hook_ctools_content_subtype_alter() to modify one particular subtype, but it's not very useful because the information passed to the hook does not provide any reliable way to identify which subtype I'm working with.

The hook should be changed to pass in the subtype ID as well.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

David_Rothstein’s picture

Status: Active » Needs review
FileSize
2.08 KB

Here is a patch. This also modifies the hook documentation to provide a more realistic example that targets a particular subtype.

The API.txt file still lists the old hook signature. Not sure if this patch should change that (or if the API version should technically be bumped)... maybe it should.

joelstein’s picture

Status: Needs review » Reviewed & tested by the community

This patch is SO useful and still applies cleanly. Thank you! Let's commit it.

plopesc’s picture

+1

I'm also using this useful patch because I need to modify a specific entity_field subtype.

Thanks!

dawehner’s picture

Is there a particular reason why we don't just merge the sub plugin ID directly into $subtype,
so the example code would look like this:

function hook_ctools_content_subtype_alter(&$subtype) {
  // Force a particular subtype of a particular plugin to render last.
  if ($plugin['module'] == 'some_plugin_module' && $plugin['name'] == 'some_plugin_name' && $subtype['subtype_id'] == 'my_subtype_id') {
    $subtype['render last'] = TRUE;
  }
 }
dawehner’s picture

In general though I don't feel strong about that.

Fabianx’s picture

RTBC + 1

joelstein’s picture

@dawehner: I'm okay with either solution, as long as it gets committed. :)

kevinsiji’s picture

jenlampton’s picture

+1 on RTBC from me. thanks @David_Rothstein!

rivimey’s picture

Patch applies cleanly to 7.x-1.x.

Code looks good and is 'obvious', with one exception. Would it be sensible to put a default value on the function, thus:

function ctools_content_prepare_subtype(&$subtype, $plugin, $subtype_id = '')

so that should some custom or contrib module code call this function, the value of $subtype_id (and thus the value passed to any hooked-in code) is defined? I guess at present it will get the value NULL; is that ok?

Other than that, good to go.

rivimey’s picture

Issue tags: +Dublin2016
rivimey’s picture

japerry’s picture

Status: Reviewed & tested by the community » Needs work

So this function seems to me like an API change, which doesn't seem like a good idea this far into d7. I don't mind going with dawehner 's suggestion in #5 though if someone wants to code up a patch for it.

DamienMcKenna’s picture

Status: Needs work » Needs review
FileSize
1.41 KB

Like this?

DamienMcKenna’s picture

Fabianx’s picture

Status: Needs review » Reviewed & tested by the community

RTBC to #15

japerry’s picture

Status: Reviewed & tested by the community » Fixed

Yah, thats better. Fixed!

joelstein’s picture

Thanks for committing this!

However, I found a couple scenarios where the subtype_id is still not being set. I opened a follow-up issue (with patch) at #2829782: hook_ctools_content_subtype_alter() does not *always* provide reliable way to identify the subtype being altered.

ecsabi’s picture

Hi, #1 was a life saver as it allowed me to use fivestar module in panels.
The patch worked nicely until CTOOLS 7.x-1.11.
Yesterday they released 7.x-1.12 which cannot be amended by the patch.
Could this patch be updated somehow please?
Thanks.

joelstein’s picture

Hi ecsabi, you need to update your code to reference $subtype['subtype_id'] instead of $subtype_id. Also, you'll probably want the follow-up patch at #2829782: hook_ctools_content_subtype_alter() does not *always* provide reliable way to identify the subtype being altered.

ecsabi’s picture

I downloaded patch #1 and replaced all $subtype_id with $subtype['subtype_id']. Then I applied the patch. I'm still getting the same error (see below)

Checking patch ctools.api.php...
error: while searching for:
* This hook can be used to add things like 'render last' or change icons
* or categories or to rename content on specific sites.
*/
function hook_ctools_content_subtype_alter($subtype, $plugin) {
$subtype['render last'] = TRUE;
}

/**

error: patch failed: ctools.api.php:198
error: ctools.api.php: patch does not apply
Checking patch includes/content.inc...
error: while searching for:
// retained.
foreach ($subtypes as $id => $subtype) {
// Use exact name since this is a modify by reference.
ctools_content_prepare_subtype($subtypes[$id], $plugin);
}

$cache[$plugin['name']] = $subtypes;

error: patch failed: includes/content.inc:156
error: includes/content.inc: patch does not apply

DamienMcKenna’s picture

@escabi: Please stop trying to apply patches from this issue, the patch has already been committed so it's already in the new 1.12 release.

If you are still having problems with your custom code, please look at joelstein's comment above and please try out the patch in #2829782: hook_ctools_content_subtype_alter() does not *always* provide reliable way to identify the subtype being altered.

ecsabi’s picture

Thank you both.

I used the new patch. The error is still there:

Undefined offset: 1 in fivestar_ctools_content_subtype_alter() (line 1023 of /home/sites/mysite.co.uk/public_html/sites/all/modules/fivestar/fivestar.module)

rivimey’s picture

@ecsabi please tell us exactly what you are doing. For example:

  1. If you are using modules (such as fivestar) that interact with ctools, please tell us which version of the modules and whether they have been patched (and what with). Note that the stable current version (7.x-2.2) of 'fivestar.module' doesn't have 1023 lines or a function fivestar_ctools_content_subtype_alter()
  2. Have you updated to ctools 7.x-1.12? If not, please update to it and attempt what you need to do without any further changes to ctools.
  3. What is ctools-7.x-1.12 doing that does not meet your expectation?
  4. Which specific patch (URL) or patches (URLs, in the order you tried) have you (tried to) apply? If so, please indicate what happened and at what stage.
DamienMcKenna’s picture

@escabi: That error is in Fivestar, not CTools. I don't see fivestar_ctools_content_subtype_alter() in the current Fivestar codebase, so that must come from a patch. Please check with the Fivestar patches you're using, see which of them adds that function and report in the corresponding issue that it needs to be updated.

Status: Fixed » Closed (fixed)

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