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:
Needs to be:
You can see the block working and not working athttp://www.kat10.net
Any clever coders out there have any ideas?
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?
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?
How would you make the path automatically target the blocks folder no matter where concrete had been installed?
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:
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');
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?
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?
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?
I can't see anything wrong on your site?
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:
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?
<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?
is your code available somewhere? I never had such problems, addHeaderItem always worked fine for me..
Code for both versions attached.
Be really interested to hear what you think.
Be really interested to hear what you think.
where do you include the css??
I can only see JavaScript files.. Will look at it again after lunch
I can only see JavaScript files.. Will look at it again after lunch
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.
Guess I'm blind..
v2, on_page_view:
I can't find ".css" there?
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?
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.
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.
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
There was a discussion in the beta section about this.
"js" should work as well I think
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_...
http://www.concrete5.org/developers/beta/beta_bitching/beta_update_...
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>
<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>
looks like a bug to me then...
are you using the latest c5 version?
are you using the latest c5 version?
Fresh install of 5.3.3.1
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 "/"