You are here

public static function XBBCodeHandlerForm::buildFormHandlers in Extensible BBCode 8.2

Generate the handler subform.

2 calls to XBBCodeHandlerForm::buildFormHandlers()
XBBCodeFilter::settingsForm in src/Plugin/Filter/XBBCodeFilter.php
Settings callback for the filter settings of xbbcode.
XBBCodeHandlerForm::buildForm in src/Form/XBBCodeHandlerForm.php
Form constructor.

File

src/Form/XBBCodeHandlerForm.php, line 51
Contains \Drupal\xbbcode\Form\XBBCodeTagForm.

Class

XBBCodeHandlerForm

Namespace

Drupal\xbbcode\Form

Code

public static function buildFormHandlers(array $form, array $defaults) {
  module_load_include('inc', 'xbbcode');
  $handlers = _xbbcode_build_handlers();
  $form['handlers'] = [
    '#type' => 'fieldset',
    '#tree' => FALSE,
    '#theme' => 'xbbcode_settings_handlers_format',
    '#attached' => [
      'library' => [
        'xbbcode/handlers-table',
      ],
    ],
    '#title' => t('Tag settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  ];

  // We need another #tree element named "tags" to provide a hierarchy for
  // the module selection menus.
  $form['handlers']['extra']['#tree'] = FALSE;
  $form['handlers']['extra']['tags']['#tree'] = TRUE;
  $form['handlers']['tags'] = [
    '#type' => 'tableselect',
    '#header' => [
      'tag' => t('Name'),
      'description' => t('Description'),
      'module' => t('Module'),
    ],
    '#default_value' => [],
    '#options' => [],
    '#attributes' => [
      'id' => 'xbbcode-handlers',
    ],
    '#empty' => t('No tags or handlers are defined. Please <a href="@modules">install a tag module</a> or <a href="@custom">create some custom tags</a>.', [
      '@modules' => Drupal::url('system.modules_list', [], [
        'fragment' => 'edit-modules-extensible-bbcode',
      ]),
      '@custom' => Drupal::url('xbbcode.admin_tags'),
    ]),
    // The #process function pushes each tableselect checkbox down into an
    // "enabled" sub-element.
    '#process' => [
      [
        'Drupal\\Core\\Render\\Element\\Tableselect',
        'processTableselect',
      ],
      'xbbcode_settings_handlers_process',
    ],
    // Don't aggregate the checkboxes.
    '#value_callback' => NULL,
  ];
  foreach ($handlers as $name => $handler) {
    if (!array_key_exists($name, $defaults)) {
      $defaults[$name] = [
        'enabled' => FALSE,
        'module' => NULL,
      ];
    }
    $form['handlers']['tags']['#options'][$name] = [
      'tag' => [
        'data' => "[{$name}]",
        'class' => [
          'xbbcode-tag-td',
        ],
      ],
      'description' => [
        'data' => [
          '#markup' => _xbbcode_build_descriptions($name, $handler['info'], $defaults[$name]['module']),
        ],
        'class' => [
          'xbbcode-tag-description',
        ],
      ],
      'module' => [
        'data' => 'handler-select',
        'class' => [
          'xbbcode-tag-td',
        ],
      ],
      '#attributes' => [
        'class' => $defaults[$name]['enabled'] ? [
          'selected',
        ] : [],
      ],
    ];
    $form['handlers']['tags']['#default_value'][$name] = $defaults[$name]['enabled'] ? 1 : NULL;
    if (count($handler['modules']) > 1) {
      $module_selector = [
        '#type' => 'select',
        '#options' => $handler['modules'],
        '#default_value' => $defaults[$name]['enabled'],
        '#attributes' => [
          'class' => [
            'xbbcode-tag-handler',
          ],
        ],
      ];
    }
    else {
      $module_selector = [
        'shown' => [
          '#type' => 'markup',
          '#markup' => current($handler['modules']),
        ],
        '#type' => 'value',
        '#value' => key($handler['modules']),
      ];
    }
    $form['handlers']['extra']['tags'][$name]['module'] = $module_selector;
  }
  return $form;
}