Rad sa konfiguracionim formama
Upotreba $config u kontekstu forme
Možete koristiti konfiguracione forme da biste saznali kako $config može dohvatiti podatke unete od strane korisnika i izmeniti podatke u fajlu {module}.settings.yml. Evo koda za deklarisanje objekta $config u formi, koji možete pronaći u PHP fajlu podešavanja forme.
Klasa Drupal Core ConfigFactory je način za čitanje i pisanje konfiguracionih podataka, i koristi se za kreiranje instance Config objekta na osnovu sadržaja određenog konfiguracionog fajla. Novi Config objekat zatim može da se koristi za izvođenje CRUD operacija sa tim podacima.
Primer definicije forme (nalazi se u example/src/Form/exampleSettingsForm.php):
namespace Drupal\example\Form; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; /** * Konfiguriše example podešavanja za ovaj sajt. */ class ExampleSettingsForm extends ConfigFormBase { /** * Podešavanja konfiguracije. * * @var string */ const SETTINGS = 'example.settings'; /** * {@inheritdoc} */ public function getFormId() { return 'example_admin_settings'; } /** * {@inheritdoc} */ protected function getEditableConfigNames() { return [ static::SETTINGS, ]; } /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config(static::SETTINGS); $form['example_thing'] = [ '#type' => 'textfield', '#title' => $this->t('Stvari'), '#default_value' => $config->get('example_thing'), ]; $form['other_things'] = [ '#type' => 'textfield', '#title' => $this->t('Druge stvari'), '#default_value' => $config->get('other_things'), ]; return parent::buildForm($form, $form_state); } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // Preuzmi konfiguraciju. $this->configFactory->getEditable(static::SETTINGS) // Postavi poslatu konfiguraciju. ->set('example_thing', $form_state->getValue('example_thing')) // Možete postaviti više konfiguracija odjednom pozivajući set() više puta. ->set('other_things', $form_state->getValue('other_things')) ->save(); parent::submitForm($form, $form_state); } }
Fajl rutiranja (example.routing.yml):
example.settings: path: '/admin/config/example/settings' defaults: _form: '\Drupal\example\Form\ExampleSettingsForm' _title: 'example' requirements: _permission: 'administer site configuration'
Korišćenjem Config objekta, možete pojednostaviti podatke koje prikupljate iz forme. Sa gore navedenim kodom u fajlu podešavanja forme, moći ćete da skladištite podatke forme u {module}.settings.yml.
Bilo koja klasa koja nasleđuje ConfigFormBase mora implementirati metodu getEditableConfigNames i vratiti niz imena konfiguracionih polja koje uređuje.
Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.