public function TagSetForm::form in Extensible BBCode 4.0.x
Same name and namespace in other branches
- 8.3 src/Form/TagSetForm.php \Drupal\xbbcode\Form\TagSetForm::form()
Gets the actual form array to be built.
Overrides EntityForm::form
See also
\Drupal\Core\Entity\EntityForm::processForm()
\Drupal\Core\Entity\EntityForm::afterBuild()
File
- src/
Form/ TagSetForm.php, line 78
Class
- TagSetForm
- Base form for tag sets.
Namespace
Drupal\xbbcode\FormCode
public function form(array $form, FormStateInterface $form_state) : array {
$form = parent::form($form, $form_state);
$form = $this
->addLabelFields($form);
// Begin by creating a table with checkboxes for each plugin.
$form['_tags'] = [
'#type' => 'tableselect',
'#title' => $this
->t('Tags'),
'#header' => [
'name' => $this
->t('Tag name'),
'label' => $this
->t('Plugin'),
],
'#options' => [],
'#empty' => $this
->t('No custom tags or plugins are available.'),
];
/** @var \Drupal\xbbcode\Entity\TagSetInterface $tagSet */
$tagSet = $this->entity;
$plugins = new TagPluginCollection($this->pluginManager, $tagSet
->getTags());
$available = $this->pluginManager
->getDefinedIds();
$settings = [];
// Add the fields for the activated plugins, keyed by current tag name.
// (This is because the same plugin might be active with multiple names.)
foreach ($plugins as $name => $plugin) {
/** @var \Drupal\xbbcode\Plugin\TagPluginInterface $plugin */
$settings["enabled:{$name}"] = $this
->buildRow($plugin, TRUE);
$form['_tags']['#default_value']["enabled:{$name}"] = TRUE;
// Exclude already enabled plugins from the bottom part of the table.
unset($available[$plugin
->getPluginId()]);
}
// Add the fields for the available plugins, keyed by plugin ID.
// (This is because multiple plugins might use the same default tag name.)
foreach ($available as $plugin_id) {
/** @var \Drupal\xbbcode\Plugin\TagPluginInterface $plugin */
try {
$plugin = $this->pluginManager
->createInstance($plugin_id);
$settings["available:{$plugin_id}"] = $this
->buildRow($plugin, FALSE);
} catch (PluginException $exception) {
// If the plugin is broken, log it and don't show it.
watchdog_exception('xbbcode', $exception);
}
}
// Add placeholders in the tableselect.
foreach ($settings as $key => $row) {
foreach ((array) $row as $name => $cell) {
$form['_tags']['#options'][$key][$name]['data'] = $name;
}
}
// Put the settings forms into a virtual _settings subkey.
$form['_settings'] = $settings;
$form['_settings']['#tree'] = TRUE;
// Move the settings from there into the tableselect rows when rendering.
$form['#pre_render'][] = static function (array $form) : array {
$table =& $form['_tags'];
$settings = $form['_settings'];
foreach (Element::children($settings) as $key) {
foreach ((array) $settings[$key] as $name => $cell) {
$table['#options'][$key][$name]['data'] = $cell;
}
}
unset($form['_settings']);
return $form;
};
$formats = $this->formatStorage
->getQuery()
->condition('filters.xbbcode.status', TRUE)
->execute();
if ($formats) {
$form['formats'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Text formats'),
'#description' => $this
->t('Text formats that use this tag set.'),
'#options' => [],
];
foreach ($this->formatStorage
->loadMultiple($formats) as $id => $format) {
$form['formats']['#options'][$id] = $format
->label();
}
if (!$this->entity
->isNew()) {
$form['formats']['#default_value'] = $this->formatStorage
->getQuery()
->condition('filters.xbbcode.settings.tags', $this->entity
->id())
->execute();
}
}
return parent::form($form, $form_state);
}