public function SettingsForm::buildForm in Blog API 8
Build the form.
Parameters
array $form:
\Drupal\Core\Form\FormStateInterface $form_state:
Return value
array
Overrides ConfigFormBase::buildForm
File
- src/
Form/ SettingsForm.php, line 79
Class
- SettingsForm
- Class SettingsForm.
Namespace
Drupal\blogapi\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('blogapi.settings');
$contentTypes = $this->entityTypeManager
->getStorage('node_type')
->loadMultiple();
$ct_keys = [];
// Get all available text formats.
$plugins = filter_formats();
$filters = [];
foreach ($plugins as $format) {
$filters[$format
->id()] = $format
->get('name');
}
// Get all the available content types.
foreach ($contentTypes as $id => $type) {
$ct_keys[$id] = $type
->label();
}
// Get the default text filter from config.
$default_text_format = $config
->get('text_format');
// Get previously enabled content types.
$enabled_content_types = $config
->get('content_types');
// Get possible body and taxonomy fields for every enabled content type.
$field_storage = [];
foreach ($enabled_content_types as $ct) {
if (!empty($ct)) {
// Get all field definitions of a content type.
$definitions = $this->entityFieldManager
->getFieldDefinitions('node', $ct);
foreach ($definitions as $field) {
$id = $field
->getName();
$label = $field
->getLabel();
// If the field is a taxonomy field.
if ($this->blogapiCommunicator
->fieldIsTaxonomy($field)) {
$field_storage[$ct]['taxonomy'][$id] = $label . ' (' . $id . ')';
}
elseif ($field
->getType() == 'comment') {
$field_storage[$ct]['comment'][$id] = $label . ' (' . $id . ')';
}
elseif ($field instanceof FieldConfig) {
$field_storage[$ct]['body'][$id] = $label . ' (' . $id . ')';
}
}
}
}
// Markup element with info.
$form['blogapi_info_wrapper'] = array(
'#type' => 'fieldset',
'#title' => $this
->t('BlogAPI endpoint'),
'blogapi_info' => [
'#type' => 'markup',
'#markup' => Url::fromRoute('xmlrpc')
->setAbsolute()
->toString(),
],
);
// Element to select enabled content types.
$form['content_types'] = array(
'#type' => 'checkboxes',
'#title' => $this
->t('Select content types to manage with BlogAPI'),
'#options' => $ct_keys,
'#default_value' => $enabled_content_types,
'#description' => $this
->t('Select the content types available to external blogging clients via Blog API. If supported, each enabled content type will be displayed as a separate "blog" by the external client..'),
);
// Element to select default text format.
$form['text_format'] = array(
'#type' => 'radios',
'#title' => $this
->t('Select default text format'),
'#options' => $filters,
'#default_value' => $default_text_format,
'#description' => $this
->t('The selected text format will be applied to the body field if the client doesn\'t specify which text format to use.'),
);
// Create fieldsets for each content type, with radio buttons
// for selecting the body and taxonomy fields.
foreach ($field_storage as $enabled_ct => $fields) {
// Fieldset for a content type.
$form['fields_config']['fields_' . $enabled_ct] = [
'#type' => 'fieldset',
'#title' => $ct_keys[$enabled_ct],
];
// Body field select.
$form['fields_config']['fields_' . $enabled_ct]['body_' . $enabled_ct] = [
'#type' => 'radios',
'#title' => $this
->t('Body field'),
'#options' => $fields['body'],
'#default_value' => $config
->get('body_' . $enabled_ct),
'#description' => $this
->t('This field will be used to store the body text.'),
];
// Taxonomy field select.
if (isset($fields['taxonomy'])) {
$form['fields_config']['fields_' . $enabled_ct]['taxonomy_' . $enabled_ct] = [
'#type' => 'radios',
'#title' => $this
->t('Vocabulary'),
'#options' => $fields['taxonomy'],
'#default_value' => $config
->get('taxonomy_' . $enabled_ct),
'#description' => $this
->t('If possible this field will be used to store selected tags.'),
];
}
else {
$form['fields_config']['fields_' . $enabled_ct]['taxonomy_' . $enabled_ct] = [
'#markup' => $this
->t('There are not taxonomy fields available for this content type.'),
];
}
// Comment field select.
if (isset($fields['comment'])) {
$form['fields_config']['fields_' . $enabled_ct]['comment_' . $enabled_ct] = [
'#type' => 'radios',
'#title' => $this
->t('Comment'),
'#options' => $fields['comment'],
'#default_value' => $config
->get('comment_' . $enabled_ct),
'#description' => $this
->t('This field will be used as the default comment field.'),
];
}
else {
$form['fields_config']['fields_' . $enabled_ct]['taxonomy_' . $enabled_ct] = [
'#markup' => $this
->t('There are not comment fields available for this content type.'),
];
}
}
return parent::buildForm($form, $form_state);
}