Repository URL to install this package:
|
Version:
1.0.4 ▾
|
<?php
namespace Drupal\premium_maps\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class MapSettingsForm
*
* @package Drupal\premium_maps\Form
*/
class MapsSettingsForm extends ConfigFormBase {
/**
* @var \Drupal\Core\Entity\EntityTypeManager
*/
private $entityTypeManager;
/**
* MapSettingsForm constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory object.
* @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManager
* The entity type manager object.
*/
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManager $entityTypeManager) {
parent::__construct($config_factory);
$this->entityTypeManager = $entityTypeManager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('entity_type.manager')
);
}
/**
* Gets the configuration names that will be editable.
*
* @return array
* An array of configuration object names that are editable if called in
* conjunction with the trait's config() method.
*/
protected function getEditableConfigNames() {
return ['premium_maps.map_settings'];
}
/**
* Returns a unique string identifying the form.
*
* The returned ID should be a unique string that can be a valid PHP function
* name, since it's used in hook implementation names such as
* hook_form_FORM_ID_alter().
*
* @return string
* The unique string identifying the form.
*/
public function getFormId() {
return 'premium_maps_settings_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('premium_maps.map_settings');
$form = parent::buildForm($form, $form_state);
$form['api_key'] = [
'#type' => 'textfield',
'#title' => $this->t('API Key'),
'#default_value' => $config->get('api_key'),
];
$definitions = [];
foreach ($this->getAllEntityDefinitions() as $definition) {
$definitions[$definition->id()] = $definition->label();
}
$form['definitions'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Choose content type'),
'#options' => $definitions,
'#default_value' => $config->get('definitions') ?? [],
'#ajax' => [
'callback' => '::buildAjaxConfigForm',
'wrapper' => 'premium-maps-config-form',
'method' => 'replace',
'effect' => 'fade',
],
];
$form['maps_configs'] = [
'#type' => 'container',
'#attributes' => [
'id' => 'premium-maps-config-form',
],
'#tree' => TRUE,
];
$this->buildConfigurationForm($form, $form_state);
return $form;
}
protected function getAllEntityDefinitions() {
return $this->entityTypeManager
->getStorage('node_type')
->loadMultiple();
}
/**
* Build the configuration form.
*
* @param array $form
* The form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Instance of formStateInterface.
*
* @return array
* The form.
*/
public function buildConfigurationForm(array &$form, FormStateInterface $form_state) {
$selected_definitions = $form_state->getValue('definitions');
$map_config = $this->config('premium_maps.map_settings')->get('maps_config');
if ($selected_definitions === NULL) {
$selected_definitions = $this->config('premium_maps.map_settings')
->get('definitions');
}
foreach ($selected_definitions as $definition) {
if (empty($definition)) {
continue;
}
// Get the "sub-form state" and appropriate form part to send to
// buildConfigurationForm().
$definition_form = [];
if (!empty($form['maps_configs'][$definition])) {
$definition_form = $form['maps_configs'][$definition];
}
$definition_form_state = SubformState::createForSubform($definition_form, $form, $form_state);
$definition_form_state->set('definition_id', $definition);
$definition_form_state->set('maps_config', $map_config);
$form['maps_configs'][$definition] = $this->buildDefinitionConfigForm($definition_form, $definition_form_state);
$form['maps_configs'][$definition]['#type'] = 'details';
$form['maps_configs'][$definition]['#title'] = $this->t('Configure the %definition entity type', ['%definition' => $definition]);
}
return $form;
}
/**
* Build the sub form for choosing which field should be used for the map.
*
* @param array $form
* Sub form array.
* @param \Drupal\Core\Form\SubformState $form_state
* Instance of SubformState.
*
* @return array
* The form.
*/
public function buildDefinitionConfigForm($form, $form_state): array {
$bundle = $form_state->get('definition_id');
$map_config = $form_state->get('maps_config');
$field_definitions = \Drupal::service('entity_field.manager')
->getFieldDefinitions('node', $bundle);
$options = [];
/** @var \Drupal\Core\Field\FieldDefinition $field_definition */
foreach ($field_definitions as $field_key => $field_definition) {
if ($field_definition->isDisplayConfigurable('form')) {
$options[$field_key] = $field_definition->getLabel();
}
}
$form['field'] = [
'#type' => 'select',
'#title' => $this->t('Field containing coordinates'),
'#options' => $options,
'#default_value' => $map_config[$bundle],
];
return $form;
}
/**
* Handles changes to the selected datasources.
*
* @param array $form
* The current form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current form state.
*
* @return array
* The part of the form to return as AJAX.
*/
public function buildAjaxConfigForm(array $form, FormStateInterface $form_state) {
return $form['maps_configs'];
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$definitions = array_filter($form_state->getValue('definitions'));
$field = $form_state->getValue('maps_configs');
$config = $this->config('premium_maps.map_settings');
$config->set('definitions', $definitions);
$config->set('maps_config', $field);
$config->set('api_key', $form_state->getValue('api_key'));
$config->save();
parent::submitForm($form, $form_state);
}
}