*/ ?>

Creating an accordion pane in Concrete 5

PLEASE NOTE:

This was written for version 5.4.something - many years ago. It probably doesn't work for newer versions since there has been a complete overhaul of everything that runs the core and what jquery ui stuff is included.

It is kept here because I don't like deleting things, and I like remembering what I did years ago.

How to use the built-in jQuery UI to add an accordion to a page type on your concrete5 website.

For an interactive demo of how this works, go here:

http://accordiondemo.werstnet.com/

Step 1 - Create a page attribute for accordion section. 

  1. Go to dashboard/pages/attributes and add a new select attribute type
  2. Handle "accordion_sections"
  3. Name "Accordion Sections"
  4. Not searchable
  5. Allow Multiple options to be chosen
  6. Allow users to add to this list
  7. Option Order display order
  8. No need to enter options now, they will not show up on the end page anyway

Step 2 - Create a page type template in your theme

  1. Copy /yourtheme/default.php to /yourtheme/accordion.php
  2. Go to /dashboard/pages/themes
  3. Inspect your theme to see the new file and activate it

Edit the newly created file, add this where you would like the accordion to appear:

  1. <div id="accordion">
  2. <?php
  3. $sections = $c->getCollectionAttributeValue('accordion_sections');
  4. if (count($sections)>0){
  5. foreach ($sections as $opt) {?>
  6. <h3><?= $opt->value; ?></h3>
  7. <div>
  8. <?php
  9. $a = new Area($opt->value);
  10. $a->setBlockLimit(1);
  11. $a->display($c);
  12. ?>
  13. </div>
  14. <? }
  15. } ?>
  16. </div>

Step 3 - Make a controller for your page type

  1. Create /site_root/controllers/page_types/accordion.php
  2. Put this in the file
  1. <?php
  2.  
  3. defined('C5_EXECUTE') or die(_("Access Denied."));
  4.  
  5. class AccordionPageTypeController extends Controller {
  6.  
  7. public function on_start() {
  8. $html = Loader::helper('html');
  9. $this->addHeaderItem($html->css('jquery.ui.css'));
  10. $this->addHeaderItem($html->css('ccm.dialog.css'));
  11. $this->addHeaderItem($html->javascript('jquery.ui.js'));
  12. $this->addHeaderItem($html->javascript('ccm.dialog.js'));
  13. $js_string = "\n";
  14. $js_string .= "<script type='text/javascript'>\n";
  15. $js_string .= " $(function() {\n";
  16. $js_string .= " $( '#accordion' ).accordion({\n";
  17. $js_string .=" collapsible: true\n";
  18. $js_string .= " });\n";
  19. $js_string .= " });\n";
  20. $js_string .= "</script>\n";
  21. $this->addHeaderItem($js_string);
  22. }
  23. }

Step 4 - Associate your page type with the accordion_sections attribute.

  1. Go to /dashboard/pages/types
  2. Click "Edit" on accordion
  3. Click the checkbox next to accordion sections
  4. Click "Update Page Type"

Step 5 - Add an accordion page

  1. Go to dashboard / sitemap
  2. Click the page you want your test page to show up under and choose 'add page'
  3. Accordion for page type
  4. Add as many accordion sections as you would like to have on the page
  5. Click "Add Page"
  6. Click New Page
  7. Choose "Visit"
  8. Edit Page
  9. You should see an accordion pane with the names of your sections, and an area in each pane to add content to

Step 6 - Update your css

The UI files do not include styling for the h3 tag, so you need to add a little css to get them to look nice:

  1. #accordion h3 {
  2. padding: 5px 5px 5px 30px;
  3. }
blog comments powered by Disqus