packthub_ebook_integration-1.0.0/src/Form/EbookContentTypeForm.php
src/Form/EbookContentTypeForm.php
<?php
namespace Drupal\packt\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\packt\Plugin\EbookDefinition;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EbookContentTypeForm extends FormBase {
/**
* Constructs a new CustomFormExampleForm object.
*
* @param ConfigFactoryInterface $config_factory
* The configuration factory.
*/
public function __construct(ConfigFactoryInterface $config_factory, MessengerInterface $messenger) {
$this->configFactory = $config_factory;
$this->messenger = $messenger;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container): EbookContentTypeForm|static {
return new static(
$container->get('config.factory'),
$container->get('messenger')
);
}
public function getFormId(): string {
return "packt_ebook_content_type_form";
}
public function buildForm(array $form, FormStateInterface $form_state): array {
$configuration = new EbookDefinition();
$configs = $configuration->contentTypeDefinition();
$options =[];
foreach ($configs as $config) {
$options[$config['id']] = $config['label'];
}
$form['content_type'] = array(
'#type' => 'select',
'#title' => t('Content Types, Select any of these content types to be used for ebook creation'),
'#options' =>$options,
'#empty_option' => t('- Select -'),
'#required' => TRUE,
'#ajax' => array(
'callback' => array($this, 'ajaxCallback'),
'wrapper' => 'dynamic_fields_wrapper',
'method' => 'replace',
'effect' => 'fade',
),
);
// Dynamic fields wrapper.
$form['dynamic_fields'] = array(
'#type' => 'container',
'#attributes' => array('id' => 'dynamic_fields_wrapper'),
);
// Additional fields that will be displayed based on the select field value.
$selected_value = $form_state->getValue('content_type');
if (!empty($selected_value)) {
$this->fieldsCollection($selected_value,$configs, $form);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state): void {
$data = [];
$notNeeded = ['op','form_token','form_id','form_build_id','submit'];
foreach ($form_state->getValues() as $key=>$value) {
if(!in_array($key, $notNeeded) && !empty($value)) {
$data[$key] = $value;
}
}
$config = $this->configFactory->getEditable('packt.fields_settings');
if($config->set('field_sets', $data)->save()) {
$this->messenger->addMessage("You have successfully saved configurations");
}
else {
$this->messenger->addError("Failed to save the configuration");
}
}
/**
* Ajax callback to update dynamic fields based on the select field value.
*/
public function ajaxCallback(array &$form, FormStateInterface $form_state) {
return $form['dynamic_fields'];
}
private function fieldsCollection(string $type, array $configs, &$form): void {
$fields = [];
foreach ($configs as $config) {
if($type === $config['id']) {
$fields = $config['fields'];
break;
}
}
if(!empty($fields)) {
foreach ($fields as $field) {
$form['dynamic_fields'][$field['id']] = [
'#type' => 'select',
'#title' => $field['label'],
'#options' =>(new EbookDefinition())->packthubFields(),
'#empty_option' => t('- Select -'),
];
}
}
}
}
