My Audio Block and addHeaderitem

Permalink
I posted a block I made for a project a while ago in response to a request for something similarhttp://www.concrete5.org/community/forums/customizing_c5/music_bloc...
Quite a few people seem to be finding it useful so I'm going to test it more thoroughly and submit it to the Marketplace.

Before I can there are a couple of problems using addHeaderItem that I need help resolving...

Firstly, When multiple instances of the block are added the code is adding the javascript header items more than once each. The number of times they are repeated seems almost random. Depending on what the last one hapens to be this will stop the block working. I thought that AddHeaderItem was only supposed to add each item once?

Secondly, how can I add a path to the included files that will automatically link to the blocks/audio-player/tools folder? The way I have it set up currently works for users with root installation but requires modification when concrte is installed in a sub directory.

Code is currently:
public function on_page_view() {
         $html = Loader::helper('html');
         $this->addHeaderItem($html->javascript('/blocks/audio_player/tools/audio-player.js'));
         $this->addHeaderItem($html->javascript('/blocks/audio_player/audio-player.config.js'));
      }

Needs to be:
public function on_page_view() {
         $html = Loader::helper('html');
         $this->addHeaderItem($html->javascript('MY_PATH_TO_BLOCKS/blocks/audio_player/tools/audio-player.js'));
         $this->addHeaderItem($html->javascript('MY_PATH_TO_BLOCKS/blocks/audio_player/audio-player.config.js'));
      }


You can see the block working and not working athttp://www.kat10.net

Any clever coders out there have any ideas?

katalysis
 
Remo replied on at Permalink Reply
Remo
package id missing?

I didn't check your code but in case you created a package (which you have to do if you want to submit it to the marketplace) you would have to add the package add to ->javascript('..',$yourPkgId)

Also, I'm not sure if it's a good idea to start the location with "/"
katalysis replied on at Permalink Reply
katalysis
Not got as far as the market place Package requirements yet.

How would you make the path automatically target the blocks folder no matter where concrete had been installed?
Remo replied on at Permalink Reply
Remo
lots of options..

This is one I just saw in the block I was fixing. It's old thought - possible that there's a better way of doing it now:

$v = View::GetInstance();
         $b = $this->getBlockObject();         
         $btID = $b->getBlockTypeID();
         $bt = BlockType::getByID($btID);   
         $uh = Loader::helper('concrete/urls');
         $v->addHeaderItem('<script type="text/javascript" src="' . $uh->getBlockTypeAssetsURL($bt) . '/fancyzoom.js"></script>','CONTROLLER');
katalysis replied on at Permalink Reply
katalysis
Thanks for that Remo - it seems to sort out the problem.

However I have come up with another similar one. I've removed some css style includes from view.php to assist with validation and put them into a /css directory in the block. It all works but again the include is repeated more than once in the header.

Seehttp://www.kat6.net for updated block.

Am I right that Concrete should add this only once and also that the addHeaderItem calls we initially had problems with ought to be only added once by default? Is this a bug or am I missing something?
Remo replied on at Permalink Reply
Remo
addHeaderItem does a check for duplicate. If the content is the same it will be added once...

I can't see anything wrong on your site?
katalysis replied on at Permalink Reply
katalysis
On the latest versionhttp://www.kat6.net with the css problem the header has 2 instances of the css file:

<link rel="stylesheet" type="text/css" href="/blocks/audio_player/css/audio-player.css" />
<script type="text/javascript" src="http://www.kat6.net/blocks/audio_player/tools/audio-player.js"></script>
<script type="text/javascript"> audioPlayerDir = "http://www.kat6.net/blocks/audio_player"</script>
<script type="text/javascript" src="http://www.kat6.net/blocks/audio_player/audio-player.config.js"></script>
<link rel="stylesheet" type="text/css" href="/blocks/audio_player/css/audio-player.css" />

</head>

On the previous version prior to adding your codehttp://www.kat10.net the addheaderItem is adding multiple instances of the included javascript files:

<script type="text/javascript" src="/blocks/audio_player/tools/audio-player.js"></script>
<script type="text/javascript" src="/blocks/audio_player/audio-player.config.js"></script>
<script type="text/javascript" src="/blocks/audio_player/tools/audio-player.js"></script>
</head>


This is fixed with your suggested code but I'm interested to et to the route of the problem - is it my block somehow or something worse?
Remo replied on at Permalink Reply
Remo
is your code available somewhere? I never had such problems, addHeaderItem always worked fine for me..
katalysis replied on at Permalink Reply 2 Attachments
katalysis
Code for both versions attached.

Be really interested to hear what you think.
Remo replied on at Permalink Reply
Remo
where do you include the css??

I can only see JavaScript files.. Will look at it again after lunch
katalysis replied on at Permalink Reply
katalysis
I only use a separate css file in the second version (previously I'd put the css styles into the view.php file which of course will not validate). I found I didn't need to call it only include it in a css folder within the block directory.
Remo replied on at Permalink Reply
Remo
Guess I'm blind..

v2, on_page_view:
public function on_page_view() {
      $v = View::GetInstance();
      $b = $this->getBlockObject();         
      $btID = $b->getBlockTypeID();
      $bt = BlockType::getByID($btID);   
      $uh = Loader::helper('concrete/urls');
      $v->addHeaderItem('<script type="text/javascript" src="' . $uh->getBlockTypeAssetsURL($bt) . '/tools/audio-player.js"></script>','CONTROLLER');
      $v->addHeaderItem('<script type="text/javascript"> audioPlayerDir = "'. $uh->getBlockTypeAssetsURL($bt) . '"</script>','CONTROLLER');
      $v->addHeaderItem('<script type="text/javascript" src="' . $uh->getBlockTypeAssetsURL($bt) . '/audio-player.config.js"></script>','CONTROLLER');
      }


I can't find ".css" there?
katalysis replied on at Permalink Reply
katalysis
It's not there - I found that it was getting included anyway just by being in the css folder in the block.

I did try specifically including it using addHeaderItem but had the same issue with it being included more than once where there were multiple instances of the block on the page.
Remo replied on at Permalink Reply
Remo
ah right, this is part of the custom template functionality..

There was a discussion in the beta section about this.

"js" should work as well I think
Remo replied on at Permalink Reply
Remo
it doesn't fix your problem but if you're looking for some more information about this magic feature:

http://www.concrete5.org/developers/beta/beta_bitching/beta_update_...
katalysis replied on at Permalink Reply
katalysis
Right read the posts and tried it out onhttp://www.kat6.net. Am now loading both css and javascript by including in relevant folders and not in controller and this is the code being put into the header...

<link rel="stylesheet" type="text/css" href="/blocks/audio_player/css/audio-player.css" />
<script type="text/javascript" src="/blocks/audio_player/js/audio-player.config.js"></script>
<script type="text/javascript" src="/blocks/audio_player/js/audio-player.js"></script>
<script type="text/javascript"> audioPlayerDir = "http://www.kat6.net/blocks/audio_player"</script>
<link rel="stylesheet" type="text/css" href="/blocks/audio_player/css/audio-player.css" />
<script type="text/javascript" src="/blocks/audio_player/js/audio-player.config.js"></script>
<script type="text/javascript" src="/blocks/audio_player/js/audio-player.js"></script>
<script type="text/javascript" src="/blocks/audio_player/js/audio-player.config.js"></script>
<link rel="stylesheet" type="text/css" href="/blocks/audio_player/css/audio-player.css" />
<script type="text/javascript" src="/blocks/audio_player/js/audio-player.config.js"></script>
<script type="text/javascript" src="/blocks/audio_player/js/audio-player.js"></script>
Remo replied on at Permalink Reply
Remo
looks like a bug to me then...

are you using the latest c5 version?
katalysis replied on at Permalink Reply
katalysis
Fresh install of 5.3.3.1