public function OpenlayersLayerFormBase::buildForm in Openlayers 8.4
Overrides Drupal\Core\Entity\EntityFormController::form().
Builds the entity add/edit form.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: An associative array containing the current state of the form.
Return value
array An associative array containing the openlayers_layer add/edit form.
Overrides EntityForm::buildForm
File
- src/
Form/ OpenlayersLayerFormBase.php, line 78
Class
- OpenlayersLayerFormBase
- Class OpenlayersLayerFormBase.
Namespace
Drupal\openlayers\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
// Get anything we need from the base class.
$form = parent::buildForm($form, $form_state);
// Drupal provides the entity to us as a class variable. If this is an
// existing entity, it will be populated with existing values as class
// variables. If this is a new entity, it will be a new object with the
// class of our entity. Drupal knows which class to call from the
// annotation on our Layer class.
$ol_layer = $this->entity;
$sourceNames = \Drupal::service('config.storage')
->listAll('openlayers.openlayers_source');
$source_options = [];
foreach ($sourceNames as $sourceName) {
$sourceLabel = \Drupal::config($sourceName)
->get('label');
$sourceName = str_replace('openlayers.openlayers_source.', '', $sourceName);
$source_options[$sourceName] = $sourceLabel;
}
$type_options = [
'image' => 'Image',
'tile' => 'Tile',
'vector' => 'Vector',
];
$source_options = [];
$sources = $this->entity
->getAllSources();
foreach ($sources as $source => $definition) {
$source_options[$source] = $definition;
}
// Build the form.
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $ol_layer
->label(),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#title' => $this
->t('Machine name'),
'#default_value' => $ol_layer
->id(),
'#machine_name' => [
'exists' => [
$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' => !$ol_layer
->isNew(),
];
$form['layer_type'] = [
'#title' => t('Layer Type'),
'#type' => 'select',
'#description' => t(''),
'#options' => $type_options,
'#default_value' => isset($ol_layer->layer_type) ? $ol_layer->layer_type : null,
'#required' => true,
];
$form['source'] = array(
'#type' => 'fieldset',
'#title' => $this
->t('Source'),
);
$form['source']['source'] = [
'#title' => t('Source'),
'#type' => 'select',
'#description' => t(''),
'#options' => $source_options,
'#default_value' => isset($ol_layer->source) ? $ol_layer->source : null,
];
// Return the form.
return $form;
}