class EntityExportCsvForm in Entity Export CSV 8
Class EntityExportCsvForm.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\entity_export_csv\Form\EntityExportCsvForm uses EntityExportCsvTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of EntityExportCsvForm
File
- src/
Form/ EntityExportCsvForm.php, line 16
Namespace
Drupal\entity_export_csv\FormView source
class EntityExportCsvForm extends EntityForm {
use EntityExportCsvTrait;
/**
* The FieldTypeExportManager.
*
* @var \Drupal\entity_export_csv\Plugin\FieldTypeExportManagerInterface
*/
protected $fieldTypeExportManager;
/**
* Drupal\user\UserDataInterface definition.
*
* @var \Drupal\user\UserDataInterface
*/
protected $userData;
/**
* The entity export csv manager.
*
* @var \Drupal\entity_export_csv\EntityExportCsvManagerInterface
*/
protected $manager;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Define entity export csv form constructor.
*
* @param \Drupal\entity_export_csv\Plugin\FieldTypeExportManagerInterface $field_type_export_manager
* The field type export manager.
* @param \Drupal\entity_export_csv\EntityExportCsvManagerInterface $entity_export_csv_manager
* The entity export csv manager service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(FieldTypeExportManagerInterface $field_type_export_manager, EntityExportCsvManagerInterface $entity_export_csv_manager, LanguageManagerInterface $language_manager) {
$this->fieldTypeExportManager = $field_type_export_manager;
$this->manager = $entity_export_csv_manager;
$this->languageManager = $language_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.field_type_export'), $container
->get('entity_export_csv.manager'), $container
->get('language_manager'));
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
/** @var \Drupal\entity_export_csv\Entity\EntityExportCsvInterface $entity_export_csv */
$entity_export_csv = $this->entity;
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $entity_export_csv
->label(),
'#description' => $this
->t("Label for the Entity export csv."),
'#required' => TRUE,
'#weight' => -100,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $entity_export_csv
->id(),
'#machine_name' => [
'exists' => '\\Drupal\\entity_export_csv\\Entity\\EntityExportCsv::load',
],
'#disabled' => !$entity_export_csv
->isNew(),
'#weight' => -99,
];
$form['#attached']['library'][] = 'entity_export_csv/export_form';
$options = $this->manager
->getContentEntityTypesEnabled(TRUE);
if (empty($options)) {
$this
->messenger()
->addWarning($this
->t('No entity type have been configured to be exported.'));
return $form;
}
if ($this->languageManager
->isMultilingual()) {
$languages = $this->languageManager
->getLanguages();
$languages_options = [];
foreach ($languages as $language_id => $language) {
$languages_options[$language
->getId()] = $language
->getName();
}
$form['langcode'] = [
'#type' => 'select',
'#title' => $this
->t('Language'),
'#description' => $this
->t('Select the language you want export'),
'#options' => $languages_options,
'#default_value' => $entity_export_csv
->getLangCode() ?: $this->languageManager
->getDefaultLanguage()
->getId(),
];
}
$entity_type_id_default = $entity_export_csv
->getTargetEntityTypeId() ?: '';
$form['entity_type_id'] = [
'#type' => 'select',
'#title' => $this
->t('Entity type'),
'#required' => TRUE,
'#options' => [
'' => $this
->t('Select'),
] + $options,
'#default_value' => $entity_type_id_default,
'#ajax' => [
'event' => 'change',
'method' => 'replace',
'wrapper' => 'bundle-wrapper',
'callback' => [
$this,
'ajaxReplaceBundleCallback',
],
],
];
$form['bundle_wrapper'] = [
'#type' => 'container',
'#prefix' => '<div id="bundle-wrapper">',
'#suffix' => '</div>',
];
$entity_type_id = $this
->getElementPropertyValue('entity_type_id', $form_state, $entity_type_id_default);
$bundles = $this->manager
->getBundlesEnabledPerEntityType($entity_type_id, TRUE);
$bundle_default = $entity_export_csv
->getTargetBundle() ?: '';
if ($entity_type_id) {
$form['bundle_wrapper']['bundle'] = [
'#type' => 'select',
'#title' => $this
->t('Bundle'),
'#description' => $this
->t('Select the bundle to export.'),
'#options' => [
'' => $this
->t('- Select -'),
] + $bundles,
'#default_value' => $this
->getElementPropertyValue('bundle', $form_state, $bundle_default),
'#required' => TRUE,
'#ajax' => [
'event' => 'change',
'method' => 'replace',
'wrapper' => 'fields-wrapper',
'callback' => [
$this,
'ajaxReplaceFieldsCallback',
],
],
];
$delimiters = $this->manager
->getDelimiters();
$form['bundle_wrapper']['delimiter'] = [
'#type' => 'select',
'#title' => $this
->t('Delimiter'),
'#options' => $delimiters,
'#default_value' => $entity_export_csv
->getDelimiter() ?: ',',
];
$form['bundle_wrapper']['fields'] = [
'#type' => 'container',
'#prefix' => '<div id="fields-wrapper">',
'#suffix' => '</div>',
'#tree' => TRUE,
'#attributes' => [
'class' => [
'inline-elements',
],
],
];
$bundle = $this
->getElementPropertyValue('bundle', $form_state, $bundle_default);
if ($bundle) {
$form['bundle_wrapper']['fields']['#title'] = $this
->t('Select fields to export');
$fields = $this->manager
->getBundleFieldsEnabled($entity_type_id, $bundle, TRUE);
$fields_default_values = $entity_export_csv
->getFields() ?: [];
if (!empty($fields_default_values)) {
$this->manager
->sortNaturalFields($fields, $fields_default_values);
}
foreach ($fields as $field_name => $definition) {
$field_name_class = Html::cleanCssIdentifier($field_name);
$form['bundle_wrapper']['fields'][$field_name] = [
'#type' => 'fieldset',
'#title' => $definition
->getLabel(),
'#tree' => TRUE,
];
$enable_default = isset($fields_default_values[$field_name]['enable']) ? $fields_default_values[$field_name]['enable'] : TRUE;
$form['bundle_wrapper']['fields'][$field_name]['enable'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable'),
'#default_value' => $this
->getElementPropertyValue([
'fields',
$field_name,
'enable',
], $form_state, $enable_default),
];
$order_default = isset($fields_default_values[$field_name]['order']) ? $fields_default_values[$field_name]['order'] : 0;
$form['bundle_wrapper']['fields'][$field_name]['order'] = [
'#type' => 'number',
'#title' => $this
->t('Order'),
'#required' => TRUE,
'#default_value' => $this
->getElementPropertyValue([
'fields',
$field_name,
'order',
], $form_state, $order_default),
];
$field_type = $definition
->getType();
$exporters = $this->fieldTypeExportManager
->getFieldTypeOptions($field_type, $entity_type_id, $bundle, $field_name);
$exporter_ids = array_keys($exporters);
$default_exporter = isset($fields_default_values[$field_name]['exporter']) && isset($exporters[$fields_default_values[$field_name]['exporter']]) ? $fields_default_values[$field_name]['exporter'] : $this
->getDefaultExporterId($exporter_ids);
$default_exporter_value = $this
->getElementPropertyValue([
'fields',
$field_name,
'exporter',
], $form_state, $default_exporter);
$form['bundle_wrapper']['fields'][$field_name]['exporter'] = [
'#type' => 'select',
'#title' => $this
->t('Export format'),
'#options' => $exporters,
'#default_value' => $default_exporter_value,
'#required' => TRUE,
'#ajax' => [
'event' => 'change',
'method' => 'replace',
'wrapper' => 'fields-wrapper',
'callback' => [
$this,
'ajaxReplaceFieldsCallback',
],
],
];
$form['bundle_wrapper']['fields'][$field_name]['form'] = [
'#type' => 'container',
'#prefix' => '<div id="export-form-wrapper-"' . $field_name_class . '>',
'#suffix' => '</div>',
];
$triggering = $form_state
->getTriggeringElement();
if ($triggering['#name'] === 'fields[' . $field_name . '][exporter]') {
$default_exporter_value = $triggering['#value'];
}
// @TODO handle configuration values
$configuration_default = isset($fields_default_values[$field_name]['form']['options']) ? $fields_default_values[$field_name]['form']['options'] : [];
$configuration = $this
->getElementPropertyValue([
'fields',
$field_name,
'form',
'options',
], $form_state, $configuration_default);
/** @var \Drupal\entity_export_csv\Plugin\FieldTypeExportInterface $plugin */
$plugin = $this->fieldTypeExportManager
->createInstance($default_exporter_value, $configuration);
$form['bundle_wrapper']['fields'][$field_name]['form']['options'] = $plugin
->buildConfigurationForm([], $form_state, $definition);
}
}
}
$form['status'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enabled'),
'#default_value' => $entity_export_csv
->status(),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$entity_export_csv = $this->entity;
$status = $entity_export_csv
->save();
switch ($status) {
case SAVED_NEW:
$this
->messenger()
->addMessage($this
->t('Created the %label Entity export csv.', [
'%label' => $entity_export_csv
->label(),
]));
break;
default:
$this
->messenger()
->addMessage($this
->t('Saved the %label Entity export csv.', [
'%label' => $entity_export_csv
->label(),
]));
}
$form_state
->setRedirectUrl($entity_export_csv
->toUrl('collection'));
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->cleanValues()
->getValues();
if (empty($values['entity_type_id'])) {
$form_state
->setError($form['entity_type_id'], $this
->t('The entity type ID is mandatory. Please select one and configure the export csv configuration.'));
}
parent::validateForm($form, $form_state);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityExportCsvForm:: |
protected | property | The FieldTypeExportManager. | |
EntityExportCsvForm:: |
protected | property | The language manager. | |
EntityExportCsvForm:: |
protected | property | The entity export csv manager. | |
EntityExportCsvForm:: |
protected | property | Drupal\user\UserDataInterface definition. | |
EntityExportCsvForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
EntityExportCsvForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
1 |
EntityExportCsvForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
EntityExportCsvForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
EntityExportCsvForm:: |
public | function | Define entity export csv form constructor. | |
EntityExportCsvTrait:: |
public | function | Ajax replace callback. | |
EntityExportCsvTrait:: |
public | function | Ajax replace callback. | |
EntityExportCsvTrait:: |
public | function | Ajax replace callback. | |
EntityExportCsvTrait:: |
public | function | Ajax replace callback. | |
EntityExportCsvTrait:: |
protected | function | Get entity content export settings. | |
EntityExportCsvTrait:: |
protected | function | Get the default exporter id for a field type. | |
EntityExportCsvTrait:: |
protected | function | Get element property value. | |
EntityForm:: |
protected | property | The entity being used by this form. | 7 |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
10 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties | 7 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides FormInterface:: |
17 |
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |