Предположим, имя вашего модуля «custom», затем выполните следующие действия, чтобы создать форму администратора и извлечь сохраненную информацию на странице администратора.
Создайте папку с именем "custom".
Создайте файл "custom.info.yml" в папке "custom".
name: custom
description: Show admin saved data through custom module.
type: module
# core: 8.x
configure: admin/config/services/custom
Создайте разрешение для доступа к форме администратора.
Для разрешения создайте файл "custom.permissions.yml" в папке "custom".
'administer custom':
'title': 'Administer Customform'
'description': 'Configure how Custom Form is used on the site.'
restrict access: true
Затем создайте маршрут для пользовательского пути формы администратора и его содержимого.
Создайте файл "custom.routing.yml" в папке "custom".
custom.config:
path: '/admin/config/custom/config'
defaults:
_form: '\Drupal\custom\Form\CustomConfigForm'
_title: 'Custom Configuration'
requirements:
_permission: 'administer custom'
Теперь создайте меню и назначьте этот маршрут («custom.config») в пути меню и создайте форму в пользовательской папке, а расположение формы — src/Form/CustomConfigForm.php.
Для меню создайте файл "custom.links.menu.yml" в папке "custom".
custom.config:
title: 'Custom '
description: 'Custom Admin Configuration'
parent: system.admin_config
route_name: custom.config
weight: 100
Для формы администратора создайте файл CustomConfigForm.php в настраиваемой папке, а расположение файла — src/Form/CustomConfigForm.php.
<?php
namespace Drupal\custom\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
class CustomConfigForm extends ConfigFormBase {
public function getFormId() {
return 'custom_config_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('custom.settings'); // store data in custom.settings
$form = parent::buildForm($form, $form_state);
$form['custom_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Content Types'),
'#description' => t('Configure where the custom button should appear.'),
'#options' => node_type_get_names(),
'#default_value' => $config->get('custom_types', array()),
);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->config('custom.settings');
$config->set('custom_types', $form_state->getValue('custom_types'));
$config->save(); // save data in custom.settings
return parent::submitForm($form, $form_state);
}
public function getEditableConfigNames() {
return ['custom.settings'];
}
}
Теперь, когда вы сохраняете форму администратора, после извлечения сохраненных данных в файле «custom.module» используйте этот код.
Создайте файл "custom.module" в пользовательской папке.
$config = \Drupal::config('custom.settings'); // get saved settings
$types = $config->get('custom_types', array()); // fetch particular saved data "custom_types"
print $types;
Теперь включите этот модуль.
Путь к форме администратора: ИМЯ_САЙТА_ВАШЕГО_САЙТА/admin/config/custom/config.
Также в Drupal 8 иногда возникают проблемы с кешем, поэтому, если возникнет какая-либо проблема, очистите кеш после сохранения формы.
person
Ujjval Jha
schedule
16.04.2017