Drupal 8 - creating an accordion field

前端 未结 2 402
猫巷女王i
猫巷女王i 2021-01-15 18:32

In Drupal 8 -- I want to modify the basic-page content type to be able to support an \"accordion field type\"

I\'ve seen the list field type - that can have unlimite

2条回答
  •  终归单人心
    2021-01-15 19:16

    Drupal core sets you up well for your need--as it does several common UI requirements like accordions. You can reuse existing core assets pretty easily, and if this route meets your requirements, you'll get some admirable maintenance benefits from the fact that core gets more attention from the Drupal community than any given contrib module.

    Two ways core could help:

    • you can invoke the jQuery UI Accordion that ships with core.
    • you can pattern something on the Toolbar Menu, which "Builds a nested accordion widget".

    If the first option looks promising, the Examples module gives an example for how to use the core jQuery assets, specifically focusing on the accordion UI. (That's called serendipity incarnate!) Here's the javascript code:

    (function ($) {
    
      'use strict';
    
      $(function () {
        $('#accordion').accordion();
      });
    })(jQuery);
    

    and here's the module code:

    function js_example_theme() {
      return [
        'js_example_accordion' => [
          'template' => 'accordion',
          'variables' => ['title' => NULL],
        ],
      ];
    }
    

    Couldn't be easier. Note that if a custom module is less suited than adding the feature in your theme, you have either option available.

提交回复
热议问题