Welcome to the MetaMod and Chameleon Support Forums.

Before posting, please check out the FAQs.

helpme

 

Need extra help with your Joomla site? Consider paid Joomla support by the developer of Chameleon and MetaMod.

 

[solved] MM Pro: Recipe based on content/keywords/description fails

[solved] MM Pro: Recipe based on content/keywords/description fails

Joomla: 3.3.1
Latest Metamod Pro
YJSG Framework (latest V2 RC2) Link to Framework


Hi,

I tried several related forum topics but no solution was working.

PHP Code

Code:


$document = JFactory::getDocument();
$kw = $document->getMetaData('keywords');
$desc = $document->getMetaData('description');
$buffer = $document->getBuffer();
$content = $buffer['component'][''];
echo "Keywords: $kw <br />";
echo "Description: $desc <br />";
echo "Content starts with: $buffer<br />";

Gives following MetaMod debug info:

Module ID: 573
$option: com_content
$view: article
$id: 50
$Itemid: 435
$timezone: UTC
$language: de-de
$language_code: de
$language_region: de
Keywords: test
Description: test test
Content starts with: Array
Including modules: None


So Description and keywords are working, content not


PHP Code

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(
  "bob jones" => 29,
  "jim baker" => 22,
  "john major" => 27,
  "tony blair" => 56,
  "CREATIVE" => 19,
  "global action" => "right-metamod",
  "joomla" => 39,
  "CMS" => 33,
  "graphical user interface" => 40
);

/*****************************************/
/* 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();
$names_case = array();
foreach ($names as $key=>$module) {
  if (!$case_sensitive) {
    $key = strtolower($key);
  }
  $names_escaped[preg_quote($key)] = $module;
  $names_case[$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();

  foreach ($all[0] as $matched_name) {
    $module = $names_case[$case_sensitive ?
      $matched_name : strtolower($matched_name)];
    if ( !in_array( $module, $modules) ) {
      $modules[] = $module;
    }
  }
  if ( count($modules) > $max_num_modules ) {
    return array_slice( $modules, 0, $max_num_modules );
  }
  return $modules;
}

Gives following MetaMod debug info:

Module ID: 573
$option: com_content
$view: article
$id: 50
$Itemid: 435
$timezone: UTC
$language: de-de
$language_code: de
$language_region: de

Warning: preg_match_all() expects parameter 2 to be string, array given in .../modules/mod_metamod/helper.php(628) : eval()'d code on line 74
Including modules: None


Is it possible the same problem as in topic MetaMod Topic

I have narrowed the problem down to the JA T3 template framework. Use of this framework stops the standard method of getting the page content from working

thanks
Alexandra

mds
Beginner Modder
ranks
useravatar
Offline
2 Posts
Administrator has disabled public posting

Re: [solved] MM Pro: Recipe based on content/keywords/description fails

Hi,

I've finally found the time to do some proper testing on this - sorry about the delay. I installed the latest YJSG framework and tested it.

The following code manages to get the content:

Code:

$document = JFactory::getDocument(); 

$kw = $document->getMetaData('keywords');
$desc = $document->getMetaData('description');
$buffer = $document->getBuffer();
$content = $buffer['component'][''][''];
echo "Keywords: $kw <br />";
echo "Description: $desc <br />";
echo "Content starts with: $content<br />";

Note the extra [''] that wasn't in the original. That's important. If you fix that line in the original longer PHP excerpt I think it will probably work.

Apart from that small edit, the main thing that can prevent this recipe from working with MetaMod Pro (free is ok!)  is when for any reason the MODULES are rendered as part of the COMPONENT. This can happen if you use {loadpostion} in your articles, or if a component decides for any other reason to load modules. The reason for this is a bit complex but is basically a catch-22 situation. With MetaMod Pro, as soon as the 1st module is rendered, all the others get rendered too. If this is done in the middle of the component being rendered, obviously at that stage the component output is not complete yet, so the module can't access the final output of the component.

Hope that helps,
Stephen

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

metamodguy
useravatar
Offline
3329 Posts
User info in posts
Administrator has disabled public posting

Re: [solved] MM Pro: Recipe based on content/keywords/description fails

Hi Stephen,

I works perfectly !

thanks
Alexandra

mds
Beginner Modder
ranks
useravatar
Offline
2 Posts
Administrator has disabled public posting

Board Info

Board Stats:
 
Total Topics:
1689
Total Polls:
6
Total Posts:
5941
Posts this week:
1
User Info:
 
Total Users:
5349
Newest User:
tony55549
Members Online:
0
Guests Online:
131

Online: 
There are no members online

Forum Legend:

 Topic
 New
 Locked
 Sticky
 Active
 New/Active
 New/Locked
 New Sticky
 Locked/Active
 Active/Sticky
 Sticky/Locked
 Sticky/Active/Locked