Drupal կոնֆիգուրացիոն ձև
Դիրք IV՝ գործնական ուղեցույց Drupal 8-ի հիմնական մոդուլներ ստեղծելու համար
. info-ից մինչև թեստեր, միայն հիմունքներ
Փաստորեն ամեն ինչ բավական համաչափ է, բայց ինչպես կարող ենք փոփոխել այն, ինչ տեսնում ենք։ Կա՛մ որոշ ֆորմաների միջոցով, իհարկե։
Պարզ կիրառական սցենարներ դիտեք «Կայքի պարամետրեր և նշումներ» բաժնում։
/src/Form/LoremIpsumForm.php
<?php namespace Drupal\loremipsum\Form; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; class LoremIpsumForm extends ConfigFormBase { /** * {@inheritdoc} */ public function getFormId() { return 'loremipsum_form'; }
Մենք սկսում ենք մեր կարգավորումների ֆայլը, որը ընդլայնում է ConfigFormBase-ը։ LoremIpsumForm դասը այն դասն է, որը նշված է routing.yml ֆայլում։
Հետո տեղադրված է մեթոդ, որը իրականում կառուցում է կարգավորումների ֆորման՝
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { // Ֆորմայի կոնստրուկտոր։ $form = parent::buildForm($form, $form_state); // Նախնական կարգավորումներ։ $config = $this->config('loremipsum.settings');
$config փոփոխականը պահում է մեր ընթացիկ կարգավորումները։
Այնուհետև սահմանվում են մեր ֆորմայի դաշտերը և վերադարձվում է ֆորման՝
// Էջի վերնագրի դաշտ։ $form['page_title'] = array( '#type' => 'textfield', '#title' => $this->t('Lorem ipsum գեներատորի էջի վերնագիր՝'), '#default_value' => $config->get('loremipsum.page_title'), '#description' => $this->t('Տվեք ձեր lorem ipsum գեներատորի էջին վերնագիր։'), ); // Աղբյուրային տեքստի դաշտ։ $form['source_text'] = array( '#type' => 'textarea', '#title' => $this->t('Լորեմ իպսում գեներացման աղբյուրային տեքստ՝'), '#default_value' => $config->get('loremipsum.source_text'), '#description' => $this->t('Գրեք մեկ նախադասություն յուրաքանչյուր տողում։ Այս նախադասությունները կօգտագործվեն պատահական տեքստ ստեղծելու համար։'), ); return $form; }
Մենք պետք է նաև հայտարարենք validateForm(), submitForm() և getEditableConfigNames() մեթոդները, նույնիսկ եթե դրանք չգործածենք՝
/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $config = $this->config('loremipsum.settings'); $config->set('loremipsum.source_text', $form_state->getValue('source_text')); $config->set('loremipsum.page_title', $form_state->getValue('page_title')); $config->save(); return parent::submitForm($form, $form_state); } /** * {@inheritdoc} */ protected function getEditableConfigNames() { return [ 'loremipsum.settings', ]; } }
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.