View source
<?php
namespace Drupal\paragraphs_browser\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\paragraphs_browser\BrowserGroupList;
use Drupal\paragraphs_browser\BrowserTypeInterface;
use Drupal\paragraphs_browser\Entity\BrowserType;
class GroupEditForm extends FormBase {
protected $entity;
public function getFormId() {
return 'paragraphs_browser_group_form';
}
public function buildForm(array $form, FormStateInterface $form_state, BrowserTypeInterface $paragraphs_browser_type = null, $group_machine_name = '') {
$this->entity = $paragraphs_browser_type;
$groups = $paragraphs_browser_type
->groupManager();
$form['label'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $groups
->getGroup($group_machine_name)
->getLabel(),
'#required' => TRUE,
);
$form['id'] = array(
'#type' => 'machine_name',
'#title' => $this
->t('Machine name'),
'#default_value' => $group_machine_name,
'#machine_name' => array(
'exists' => array(
$this,
'exists',
),
'replace_pattern' => '([^a-z0-9_]+)|(^custom$)',
'error' => 'The machine-readable name must be unique, and can only contain lowercase letters, numbers, and underscores. Additionally, it can not be the reserved word "custom".',
),
'#disabled' => TRUE,
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#button_type' => 'primary',
'#submit' => array(
'::submitForm',
'::save',
),
);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$group = $this->entity
->groupManager()
->getGroup($form_state
->getValue('id'));
$group
->setLabel($form_state
->getValue('label'));
$form_state
->setRedirectUrl($this->entity
->toUrl('groups-form'));
}
public function save(array $form, FormStateInterface $form_state) {
$this->entity
->save();
}
}