Welcome to the MetaMod and Chameleon Support Forums.

Before posting, please check out the FAQs.


jodancer
Beginner Modder
ranks
useravatar
User Info

2 different modules same trigger word

Hi,

Firstly thanks for this great module!

I am trying to get 2 modules to display with the same word trigger. I have it set to $max_num_modules = 3; and it is not case sensitive.
It displays more than one module when a differnt word trigger is used but not if the same word is used.
Is there a way around this?

(example of what I have calling 2 different modules with the same word)

  "SABADELL" => 116,
  "TERRASSA" => 117,
  "GIRONA" => 118,
  "GIRONA" => 122,

Thank you


Administrator has disabled public posting
metamodguy
useravatar
User Info

Re: 2 different modules same trigger word

Sorry for the delay in replying. Here's a modified example for you. See how you can return multiple modules by putting them in quotes and separating them with commas.

Code:

// To set it up, just edit the "$source", "$case_sensitive",

// "$allow_as_substrings", "$names" and "$max_num_modules"
// variables below. The "$names" array contains the
// words/phrases that you want to search for, and the module
// ID or module position to use when any one of them is found.

$source = "content"; /* either "keywords", "description" or "content" */

$case_sensitive = false; /* set to true or false */

/* if you set $allow_as_substrings to true, then the search
* will pick up "billybob thornton" even if you are trying
* to search for "bob thornton". Set it to false to make
* sure that there's an least 1 non-alphabet character on
* each end of the string you are searching for.
*/
$allow_as_substrings = false; /* set to true or false */

$max_num_modules = 3; /* it will just give you the first 3 it finds */

/* In this array, the first part is the text to search for, and
* the last part is the module id to include if that text is
* found. Add as many as you like.
* Remember every line has to end in a comma EXCEPT the last one!
*/
$names = array(
  "SABADELL" => 116,
  "TERRASSA" => 117,
  "GIRONA" => "118,122"
);

/*****************************************/
/* you shouldn't need to edit below here */
/*****************************************/
$document =& JFactory::getDocument();

switch ($source) {
  case "keywords":
  $content = $document->getMetaData('keywords');
  break;

  case "description":
  $content = $document->getMetaData('description');
  break;

  default:
  $buffer = $document->getBuffer();
  $content = $buffer['component'][''];
}

$names_escaped = array();
foreach ($names as $key=>$module) {
  if (!$case_sensitive) {
    $key = strtolower($key);
  }
  $names_escaped[preg_quote($key)] = $module;
}
$names = $names_escaped;
$names_only = str_replace('/','\/',implode("|", array_keys($names)));

$word_boundary = $allow_as_substrings ? '' : '\b';

$result = preg_match_all("/" . $word_boundary
  . "(" . $names_only . ")"
  . $word_boundary . "/"
  . ($case_sensitive ? '' : 'i'), $content, $all);

if ($result > 0) {
  $modules = array();
  $final = array();

  foreach ($all[0] as $matched_name) {
    $module = $names[$case_sensitive ?
      $matched_name : strtolower($matched_name)];
    $modules[] = $module;
  }

  // explode comma-separated module lists
  foreach ($modules as $m) {
    $exp = explode(",",$m);
    foreach ($exp as $el) {
      if (!in_array((int)$el, $final)) $final[] = (int)$el;
    }
  }

  if ( count($final) > $max_num_modules ) {
    return array_slice( $final, 0, $max_num_modules );
  }
  return $final;
}


Stephen Brandon
MetaMod / Chameleon developer
If you use MetaMod or Chameleon, please post a rating and a review at the Joomla! Extensions Directory

Administrator has disabled public posting
jodancer
Beginner Modder
ranks
useravatar
User Info

Re: 2 different modules same trigger word

Thank you!


Administrator has disabled public posting

Board Info

Board Stats:   Total Users: 3096  Total Topics: 1195  Total Polls: 7  Total Posts: 4451  Total Posts Today: 1
User Info:   Newest User :  consyder   Members Online: 1   Guests Online: 127
Online 
Wojciech
Topic
New
Locked
Topic
New
Locked
Sticky
Active
New/Active
Sticky
Active
New/Active
New/Closed
New Sticky
Closed/Active
New/Locked
New Sticky
Locked/Active
Active/Sticky
Sticky/Locked
Sticky Active Locked
Active/Sticky
Sticky/Locked
Sticky/Active/Locked