Drupal 8 - creating an accordion field

自闭症网瘾萝莉.ら 提交于 2019-12-01 13:16:40

There is no module to handle this for you, the solution is to create unlimited Entity Reference in your content type, it must have two fields, Title and Body as you want, and to convert it to accordions you should customize new entity field theme and implement accordion there.

Another solution is, handle it by https://www.drupal.org/project/views_bootstrap which support accordion or https://www.drupal.org/project/faqfield module :

Features:
Configurable default text formats
Configurable answer widget
Types: Normal textareas, textfields and formatable textareas
Formatable textareas for any Wysiwyg editor
Configurable number of rows for textarea widget
Field formatters
jQuery Accordion UI
Simple themeable text
Definition list (HTML <dl>)
Anchor link list
Accordion display options
Choose first active question
Collapse open questions
Event to open/collapse questions (eg. mouseover, click)

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:

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!