Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

joomla change HeadData

Status
Not open for further replies.

glimbeek

Programmer
Nov 30, 2009
83
NL
Hi,

I'm trying to dynamically change parts of an array.

This is the array:
Code:
Array ( [/components/com_jcomments/js/jcomments-v2.1.js?v=7] => text/javascript [/components/com_jcomments/libraries/joomlatune/ajax.js?v=3] => text/javascript [/media/system/js/mootools.js] => text/javascript [/media/system/js/caption.js] => text/javascript )

It's a Joomla! 1.5.x website.
The I get the array by using the following code:
Code:
$headerstuff	= $this->getHeadData();  
$scripts		= $headerstuff['scripts'];
print_r ($scripts);

I want to change parts of the array because the array is used to display the following code:
Code:
  <link rel="stylesheet" href="/components/com_jcomments/tpl/custom/style.css?v=12" type="text/css" />
  <script type="text/javascript" src="/components/com_jcomments/js/jcomments-v2.1.js?v=7"></script>
  <script type="text/javascript" src="/components/com_jcomments/libraries/joomlatune/ajax.js?v=3"></script>

  <script type="text/javascript" src="/media/system/js/mootools.js"></script>
  <script type="text/javascript" src="/media/system/js/caption.js"></script>

I want to change this into:
Code:
  <link rel="stylesheet" href="[URL unfurl="true"]http://www.mydomain.com/components/com_jcomments/tpl/custom/style.css?v=12"[/URL] type="text/css" />
  <script type="text/javascript" src="[URL unfurl="true"]http://www.mydomain.com/components/com_jcomments/js/jcomments-v2.1.js?v=7"></script>[/URL]
  <script type="text/javascript" src="[URL unfurl="true"]http://www.mydomain.com/components/com_jcomments/libraries/joomlatune/ajax.js?v=3"></script>[/URL]

  <script type="text/javascript" src="[URL unfurl="true"]http://www.mydomain.com/media/system/js/mootools.js"></script>[/URL]
  <script type="text/javascript" src="[URL unfurl="true"]http://www.mydomain.com/media/system/js/caption.js"></script>[/URL]

I've been trying several things and I found a 1001 pages that tell me how to "unset" certain bits of the array, for instance http://www.eboga.org/cms/joomla/how-to-remove-mootoolsjs-and-captionjs-from-joomla15.html. I found but I can't get that to work either.

I just can't figure out how to dynamically "add" the domain to the file location. I either end up over writing the "text/javascript" bit with the domain or I end up with just one edited line and all the other lines are removed from the array.

Any help or any pointers into the right direction would be greatly appreciated.
 
your data structure is not straightforward. you will probably find there is a plugin for joomla to do this.

have you looked at the 'BASE' meta directive for html?

in php you would need to do this

Code:
foreach ($scripts as &$url=>$type):
  $url = '[URL unfurl="true"]http://www.mydomain.com'[/URL] . $url;
endforeach;

you must then do something with the $scripts array to cause joomla to output it again. perhaps setHeadData() is the right method?
 
Hi jpadie,

Thank you for your reply.
Yes, there are most likely extensions that can do part of this.
However, because I have several different scenario's on which I want to display the right scripts these extensions are no good for to me.

And yes you are right setHeadData is the way to go:
$this->setHeadData($somevar);

you must then do something with the $scripts array to cause joomla to output it again
and that's where I am struggling.
 
the conversion is using the code i suggested.

by your own statement you use setHeadData() to add $scripts back in. I believe that the right syntax would be

Code:
foreach ($this->_scripts as &$url=>$type):
  $url = '[URL unfurl="true"]http://www.mydomain.com'[/URL] . $url;
endforeach;
foreach($this->_links as &$url=>$ignore):
  $url = '[URL unfurl="true"]http://www.mydomain.com'[/URL] . $url;
endforeach;

you run this WITHIN the $this context and it will directly manipulate the headers that exist
 
no idea what you're talking about.

let's recap.

in your first post you said this

The I get the array by using the following code:
Code:
$headerstuff    = $this->getHeadData();  
$scripts        = $headerstuff['scripts'];
print_r ($scripts);

I note that you obtain the $headerstuff variable within an object context that has the method getHeadData(). I assume that this is the object context that would actually contains the headers links etc. either a document or the html object.

so instead of using that code above, use the code that I posted in my previous post. this will _directly_ manipulate the holding variables within the relevant object context. that should be all you need.
 
I lost you...

Could you give me a code example because I'm unsure what to use and how to us it now.

in my index.php of my Joomla! template I "get" the scripts from the head data by using:
Code:
$headerstuff    = $this->getHeadData();  
$scripts        = $headerstuff['scripts'];

Now you tell me to drop that and use
Code:
foreach ($this->_scripts as &$url=>$type):
  $url = '[URL unfurl="true"]http://www.mydomain.com'[/URL] . $url;
endforeach;
foreach($this->_links as &$url=>$ignore):
  $url = '[URL unfurl="true"]http://www.mydomain.com'[/URL] . $url;
endforeach;

But how?
 
Hi jpadie,

I hate to sound like a complete novice, but you've lost me here:

I'm trying the following code now:
Code:
$headerstuff	= $this->getHeadData();  
foreach ($this->_scripts as &$url=>$type):
  $url = '[URL unfurl="true"]http://www.mydomain.com'[/URL] . $url;
endforeach;
foreach($this->_links as &$url=>$ignore):
  $url = '[URL unfurl="true"]http://www.mydomain.com'[/URL] . $url;
endforeach;
$doc->setHeadData($headerstuff);

Obviously that won't work. How do I get the $url into the right place in the array so I can use $doc->setHeadData
 
exactly like I said. delete the code you had and replace with my code.

or now that you have changed things,
delete the lines shown

Code:
[s][red]$headerstuff    = $this->getHeadData();  [/red][/s]
foreach ($this->_scripts as &$url=>$type):
  $url = '[URL unfurl="true"]http://www.mydomain.com'[/URL] . $url;
endforeach;
foreach($this->_links as &$url=>$ignore):
  $url = '[URL unfurl="true"]http://www.mydomain.com'[/URL] . $url;
endforeach;
[s][red]$doc->setHeadData($headerstuff);[/red][/s]
 
jpadie,

If I try that I get the following result:

Fatal error: Key element cannot be a reference in /home/adminyu5/public_html/myfolder/templates/thistemplate/index.php on line 73

I Googled the error, but I have no idea what it means.
 
ok. try this then, instead

Code:
foreach ($this->_scripts as $url=>$type):
  $_url = '[URL unfurl="true"]http://www.mydomain.com'[/URL] . $url;
  unset($this->_scripts[$url];
  $_scripts[$_url] = $type;
endforeach;
foreach($this->_links as $url=>$ignore):
  $_url = '[URL unfurl="true"]http://www.mydomain.com'[/URL] . $url;
  unset($this->_links[$url];
  $_links[$_url] = $ignore;
endforeach;
$this->scripts = array_merge($this->scripts, $_scripts);
$this->_links = array_merge($this->links, $_links);
 
Hi jpadie,

Thank you for putting in the time and effort to help me out. If I try the above code (I had to add 2 closing ")" after the unset lines, otherwise it wouldn't work) I get the following errors:

Code:
Notice: Undefined property: JDocumentHTML::$scripts in index.php on line 81

Warning: array_merge() [function.array-merge]: Argument #1 is not an array in index.php on line 81

Notice: Undefined property: JDocumentHTML::$links in index.php on line 82

Notice: Undefined variable: _links in index.php on line 82

Warning: array_merge() [function.array-merge]: Argument #1 is not an array in index.php on line 82

Warning: array_merge() [function.array-merge]: Argument #2 is not an array in index.php on line 82

Warning: Invalid argument supplied for foreach() in head.php on line 195

Hopefully you can help me out.
 
With help I managed to solve it in the end:

$headerstuff =& $this->getHeadData();
$scripts =& $headerstuff['scripts'];
$nscripts = array();
$matches = array(
"mootools.js" => " "caption.js" => " );

foreach($scripts as $k => $v){
$scriptname = array_pop(explode("/", $k));
if(array_key_exists($scriptname, $matches)){
$nscripts[$matches[$scriptname].$k] = $v;
} else {
$nscripts[$k] = $v;
}
}

$headerstuff['scripts'] = $nscripts;
$this->setHeadData($headerstuff);

Thanks for your help jpadie!
 
I was typing into an iPhone. The ->scripts and links should be prefixed with an underscore. ->_scripts etc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top