Hi,

I set an argument passed to the view inside a panel as a string (because it's multi-word).
I also need to use that argument in the page title but with first letters of words uppercased.
So, I modified the file ctools/plugins/contexts/string.inc to make this string substitution available.

$plugin = array(
  'title' => t('String'),
  'description' => t('A context that is just a string.'),
  'context' => 'ctools_context_create_string',
  'keyword' => 'string',
  'no ui' => TRUE,
  'context name' => 'string',
  'convert list' => array(
    'raw' => t('Raw string'),
    'html_safe' => t('HTML-safe string'),
	'uppercase_words_HTML_safe' => t('HTML-safe string, first letters uppercased'),
  ),
  'convert' => 'ctools_context_string_convert',
  'placeholder form' => array(
    '#type' => 'textfield',
    '#description' => t('Enter the string for this context.'),
  ),
);

function ctools_context_string_convert($context, $type) {
  switch ($type) {
    case 'raw':
      return $context->data;
    case 'html_safe':
      return check_plain($context->data);
    case 'html_safe':
      return ucwords(str_replace("-", " ", check_plain($context->data)));
  }
}

This works, but I like to avoid hacking core files whenever possible.

Is there a way (hook or something) to put this transformation in a custom module where all of my other customizations reside, thus avoiding hacking core ctools files?

Thank you.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Max_Headroom’s picture

Thanks for this.
I guess like you, I have been searching for a solution to do this without having to hack ctools.

Just an error in your code:

case 'html_safe':
      return ucwords(str_replace("-", " ", check_plain($context->data)));

should be

case 'uppercase_words_HTML_safe':
      return ucwords(str_replace("-", " ", check_plain($context->data)));

Please if somebody have a solution without hacking ctools. Let us know. Maybe we will learn something :)

merlinofchaos’s picture

Why don't you submit a patch instead? This seems useful.

kobee’s picture

Version: 6.x-1.8 » 7.x-1.3
Status: Active » Needs review
FileSize
764 bytes

Indeed this is very useful for SEO.

Here I made a patch for 7.x

Max_Headroom’s picture

Status: Needs review » Reviewed & tested by the community

Tested.
I guess it does not need many tests for such a small patch.

kobee’s picture

Yes, tested on production too.

Can you commit please?

Max_Headroom’s picture

Please can we have this committed?

japerry’s picture

Status: Reviewed & tested by the community » Fixed

Sounds good. Committed.

  • japerry committed 4038406 on 7.x-1.x authored by kobee
    Issue #1163168 by kobee: Custom string context substitution
    

Status: Fixed » Closed (fixed)

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