Hello, this might have an obvious answer, but it's alluding me...
I want to override a function of a contrib module in Drupal 6. The standard approach is to copy the function code into your template.php, make the changes and rename the function from theme_foo_module_function to mytheme_foo_module_function, but what do you do when the function you want to override isnt named theme_foo_module_function but just foo_module_function?
That is I want to override the dev version of Integrated Metatags' int_meta_show_tags() function, modifying this quick patch to instead handle Facebook's Open Graph properties.
e.g. to go from this:
function int_meta_show_tags($tag) {
static $types = array();
if (is_array($tag)) {
// Cycle through each tag individually
foreach ($tag as $curr) {
int_meta_show_tags($curr);
}
} elseif ($tag->status) {
if (!isset($types[$tag->type])) {
$types[$tag->type] = int_meta_load_type($tag->type);
}
if (!empty($tag->values) || $types[$tag->type]->empty) {
if (!is_array($tag->values)) {
$tag->values = array($tag->values);
}
// If we shouldn't show empty tags, do a check here
if (!$types[$tag->type]->empty && !strlen(trim(implode('', $tag->values)))) {
return;
}
// Cleanup the data so we don't accidentally break anything
$name = check_plain(strip_tags(!empty($tag->name) ? $tag->name : $tag->field));
foreach ($tag->values as $key => $value) {
$value = check_plain(preg_replace('/[\n\r]+/', ' ', strip_tags(trim($value))));
if ($tag->combine) {
$tag->values[$key] = $value;
} else {
$value = int_meta_trim($name, $value);
drupal_set_html_head('<meta name="' . $name . '" content="' . $value . '" />');
}
}
if ($tag->combine) {
$value = int_meta_trim($name, implode(', ', array_unique($tag->values)));
drupal_set_html_head('<meta name="' . $name . '" content="' . $value . '" />');
}
}
to this:
function int_meta_show_tags($tag) {
static $types = array();
if (is_array($tag)) {
$nameAttribute = 'name';
if (stripos($name, 'og:') === 0) {
$nameAttribute = 'og';
$name = substr($name, strlen('og:'));
}
// Cycle through each tag individually
foreach ($tag as $curr) {
int_meta_show_tags($curr);
}
} elseif ($tag->status) {
if (!isset($types[$tag->type])) {
$types[$tag->type] = int_meta_load_type($tag->type);
}
if (!empty($tag->values) || $types[$tag->type]->empty) {
if (!is_array($tag->values)) {
$tag->values = array($tag->values);
}
// If we shouldn't show empty tags, do a check here
if (!$types[$tag->type]->empty && !strlen(trim(implode('', $tag->values)))) {
return;
}
// Cleanup the data so we don't accidentally break anything
$name = check_plain(strip_tags(!empty($tag->name) ? $tag->name : $tag->field));
foreach ($tag->values as $key => $value) {
$value = check_plain(preg_replace('/[\n\r]+/', ' ', strip_tags(trim($value))));
if ($tag->combine) {
$tag->values[$key] = $value;
} else {
$value = int_meta_trim($name, $value);
drupal_set_html_head('<meta ' . $nameAttribute . '="' . $name . '" content="' . $value . '" />');
}
}
if ($tag->combine) {
$value = int_meta_trim($name, implode(', ', array_unique($tag->values)));
drupal_set_html_head('<meta ' . $nameAttribute . '="' . $name . '" content="' . $value . '" />');
}
}
}
}
I could just patch the module's files directly, but it would seem to be more sensible to somehow override it? Is the theme's template.php even the right place to do this, for instance should I create my own little module that overrides this function, if so could you please point me in the right direction on how to do so?
Thank you.
Comments
Drupal override function module
Hi Piers,
Unfortunately there's not easy way to override regular PHP functions- as described in your case. Drupal's hook and theme functions are clever workarounds to this common "language feature" in PHP.
However the good news is that you can get around it by not hacking any core/conrib files with this module:
http://drupal.org/project/drupal_override_function
It will depend whether you have access to install the prerequisites for it on the server as to whether this will work for you. I can't say that I've had experience using it, so good luck. I'll be interested in knowing how you get on as I've come across similar challenges before and although considering this module as a solution, I've always found a more suitable workaround that felt less hacky.
Barry
--
Barry Fisher
Pivale
Personally I'd just hack it
Personally I'd just hack it and keep track of your changes (this is Drupal ;-) ), but if you're being good I suppose you could find all the places where the function is called and override them with the function name changed for your new one. Would that work for you?