Skip to content

Instantly share code, notes, and snippets.

@cyberlex404
Last active September 18, 2020 14:05
Show Gist options
  • Save cyberlex404/305095d3a37a47035b88aecacca134e6 to your computer and use it in GitHub Desktop.
Save cyberlex404/305095d3a37a47035b88aecacca134e6 to your computer and use it in GitHub Desktop.
Drupal 8 Form API
<?php
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// todo: add to schema.
$form['sync'] = [
'#type' => 'details',
'#title' => $this->t('Synchronization settings'),
'#open' => TRUE,
];
$form['sync']['rules'] = [
'#type' => 'details',
'#title' => $this->t('Rules'),
'#required' => TRUE,
'#open' => TRUE,
'#tree' => TRUE,
];
$form['sync']['rules']['import'] = [
'#type' => 'textarea',
'#title' => $this->t('Import rule'),
'#default_value' => $this->config('pim.sync')->get('rules.import'),
'#rows' => 10,
];
$form['sync']['rules']['publication'] = [
'#type' => 'textarea',
'#title' => $this->t('Publication rule'),
'#default_value' => $this->config('pim.sync')->get('rules.publication'),
'#rows' => 10,
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$expressionLanguage = new ExpressionLanguage();
$import = $form_state->getValue(['rules', 'import']);
if ($expressionLanguage->evaluate($import)) {
$form_state->setErrorByName('rules][import', $this->t('The phone number is too short. Please enter a full phone number.'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment