class ContentSingleExportForm in Content Synchronization 8
Same name and namespace in other branches
- 8.2 src/Form/ContentSingleExportForm.php \Drupal\content_sync\Form\ContentSingleExportForm
- 3.0.x src/Form/ContentSingleExportForm.php \Drupal\content_sync\Form\ContentSingleExportForm
Provides a form for exporting a single content file.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\content_sync\Form\ContentSingleExportForm
Expanded class hierarchy of ContentSingleExportForm
1 string reference to 'ContentSingleExportForm'
File
- src/
Form/ ContentSingleExportForm.php, line 20
Namespace
Drupal\content_sync\FormView source
class ContentSingleExportForm extends FormBase {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity bundle manager.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfo
*/
protected $entityBundleManager;
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManager
*/
protected $entityFieldManager;
/**
* Constructs a new ContentSingleImportForm.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* entity type manager
*
* @param \Drupal\Core\Entity\EntityTypeBundleInfo $entity_bundle_manager
* entity bundle manager
*
* @param \Drupal\Core\Entity\EntityFieldManager $entity_field_manager
* entity field manager
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfo $entity_bundle_manager, EntityFieldManager $entity_field_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->entityBundleManager = $entity_bundle_manager;
$this->entityFieldManager = $entity_field_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('entity_type.bundle.info'), $container
->get('entity_field.manager'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'config_single_export_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $content_type = 'node', $content_name = NULL, $content_entity = NULL) {
$entity_types = [];
$entity_type_definitions = $this->entityTypeManager
->getDefinitions();
foreach ($entity_type_definitions as $entity_type => $definition) {
if ($definition instanceof ContentEntityType) {
$entity_types[$entity_type] = $definition
->getLabel();
}
}
uasort($entity_types, 'strnatcasecmp');
$content_types = $entity_types;
$form['content_type'] = [
'#title' => $this
->t('Content type'),
'#type' => 'select',
'#options' => $content_types,
'#default_value' => $content_type,
'#attributes' => array(
'onchange' => 'this.form.content_name.value = null; if(this.form.content_entity){ this.form.content_entity.value = null; } this.form.export.value = null; this.form.submit();',
),
];
$default_type = $form_state
->getValue('content_type', $content_type);
$default_name = $form_state
->getValue('content_name', $content_name);
$form['content_name'] = [
'#title' => $this
->t('Content name'),
'#type' => 'select',
'#options' => $this
->findContent($default_type),
'#default_value' => $content_name,
'#attributes' => array(
'onchange' => 'if(this.form.content_entity){ this.form.content_entity.value = null; } this.form.export.value = null; this.form.submit();',
),
];
// Auto-complete field for the content entity
if ($default_type && $default_name) {
$form['content_entity'] = [
'#title' => $this
->t('Content Entity'),
'#type' => 'entity_autocomplete',
'#target_type' => $default_type,
'#selection_handler' => 'default',
'#selection_settings' => [
'target_bundles' => [
$default_name,
],
],
'#ajax' => [
'callback' => '::updateExport',
'wrapper' => 'edit-export-wrapper',
'event' => 'autocompleteclose',
],
];
// Remove target bundles for user & file because it is not supported.
if ($default_type == 'user' || $default_type == 'file') {
unset($form['content_entity']['#selection_settings']);
}
}
$form['export'] = [
'#title' => $this
->t('Here is your configuration:'),
'#type' => 'textarea',
'#rows' => 24,
'#prefix' => '<div id="edit-export-wrapper">',
'#suffix' => '</div>',
];
return $form;
}
/**
* Handles switching the content type selector.
*/
protected function findContent($content_type) {
$names = [
'' => $this
->t('- Select -'),
];
// For a given entity type, load all entities.
if ($content_type) {
$entity_storage = $this->entityBundleManager
->getBundleInfo($content_type);
foreach ($entity_storage as $entityKey => $entity) {
$entity_id = $entityKey;
if ($label = $entity['label']) {
$names[$entity_id] = new TranslatableMarkup('@label (@id)', [
'@label' => $label,
'@id' => $entity_id,
]);
}
else {
$names[$entity_id] = $entity_id;
}
}
}
return $names;
}
/**
* Handles switching the export textarea.
*/
public function updateExport($form, FormStateInterface $form_state) {
// Get submitted values
$entity_type = $form_state
->getValue('content_type');
$entity_bundle = $form_state
->getValue('content_name');
$entity_id = $form_state
->getValue('content_entity');
// DB entity to YAML
module_load_include('inc', 'content_sync', 'content_sync.batch');
$entity = _content_sync_db_to_entity($entity_type, $entity_bundle, $entity_id);
$output = Yaml::encode($entity);
$name = $entity_type . "." . $entity_bundle . "." . $entity['values'][0]['uuid'][0]['value'];
// Return form values
$form['export']['#value'] = $output;
$form['export']['#description'] = $this
->t('Filename: %name', [
'%name' => $name . '.yml',
]);
return $form['export'];
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Nothing to submit.
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentSingleExportForm:: |
protected | property | The entity bundle manager. | |
ContentSingleExportForm:: |
protected | property | The entity field manager. | |
ContentSingleExportForm:: |
protected | property | The entity type manager. | |
ContentSingleExportForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
ContentSingleExportForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
ContentSingleExportForm:: |
protected | function | Handles switching the content type selector. | |
ContentSingleExportForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ContentSingleExportForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
ContentSingleExportForm:: |
public | function | Handles switching the export textarea. | |
ContentSingleExportForm:: |
public | function | Constructs a new ContentSingleImportForm. | |
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 | |
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. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
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. |