View source
<?php
namespace Drupal\module_builder\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use MutableTypedData\Data\DataItem;
class ModuleNameForm extends ComponentSectionForm {
protected function getFormComponentProperties(DataItem $data) {
$component_entity_type_id = $this->entity
->getEntityTypeId();
$component_sections_handler = $this->entityTypeManager
->getHandler($component_entity_type_id, 'component_sections');
$operation = 'name';
$component_properties_to_use = $component_sections_handler
->getSectionFormComponentProperties($operation);
return $component_properties_to_use;
}
public function form(array $form, FormStateInterface $form_state) {
$module = $this->entity;
$form['name'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Readable name'),
'#maxlength' => 255,
'#default_value' => isset($module->name) ? $module->name : '',
'#description' => $this
->t("The form of the module name that appears in the UI."),
'#required' => TRUE,
);
$form['id'] = array(
'#type' => 'machine_name',
'#title' => $this
->t('Name'),
'#description' => $this
->t("The module's machine name, used in function and file names. May only contain lowercase letters, numbers, and underscores."),
'#maxlength' => 32,
'#default_value' => $module->id,
'#machine_name' => array(
'exists' => '\\Drupal\\module_builder\\Entity\\ModuleBuilderModule::load',
'source' => [
'name',
],
'standalone' => TRUE,
),
);
$form = parent::form($form, $form_state);
return $form;
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
if ($this->entity
->isNew()) {
$button = $actions['submit'];
$button['#value'] = $this
->t('Save basic information');
unset($button['#dropbutton']);
$actions = [
'submit' => $button,
];
}
return $actions;
}
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
if ($this->entity instanceof EntityWithPluginCollectionInterface) {
$values = array_diff_key($values, $this->entity
->getPluginCollections());
}
foreach ([
'name',
'id',
] as $key) {
$entity
->set($key, $values[$key]);
unset($values[$key]);
}
parent::copyFormValuesToEntity($entity, $form, $form_state);
}
}