Commit 57bfccde authored by andypost's avatar andypost Committed by larowlan

Issue #2640536 by andypost: Revert [META] Create a UI for selecting and exporting content

parent ae81658b
default_content.tabs:
class: \Drupal\Core\Menu\LocalTaskDefault
deriver: \Drupal\default_content\LocalTasks
<?php
/**
* Implements hook_entity_type_alter().
* creates another node form for exporting
*/
function __default_content_entity_type_alter(array &$entity_types) {
foreach ($entity_types as $entity_type_id => $entity_type) {
if ($entity_type instanceOf Drupal\Core\Entity\ContentEntityType) {
$entity_types[$entity_type_id]
->setFormClass('default_content_export', 'Drupal\default_content\ExportEntityForm');
$entity_types[$entity_type_id]
->setLinkTemplate('export-form', "/$entity_type_id/{{$entity_type_id}}/export");
}
}
}
/**
* Implements hook_modules_installed().
*/
......
#entity.node.export:
# defaults:
# _form: \Drupal\default_content\ExportForm
# _title: '\Drupal\defaultcontent\Export::title'
# requirements:
# _permission: 'access site reports'
# options:
# _admin_route: true
......@@ -2,9 +2,3 @@ services:
default_content.manager:
class: Drupal\default_content\DefaultContentManager
arguments: ['@serializer', '@plugin.manager.rest', '@current_user', '@entity_type.manager', '@entity.repository', '@rest.link_manager', '@event_dispatcher', '@module_handler', '@info_parser']
default_content.route.subscriber:
class: Drupal\default_content\RouteSubscriber
arguments: ['@entity_type.manager']
tags:
- { name: event_subscriber }
<?php
/**
* @file
* Contains \Drupal\default_content\ExportForm.
*/
namespace Drupal\default_content;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpFoundation\Response;
/**
* Converts a node to a config entity and shows it on screen
* We use the EntityForm here because it is an easy way to get the node from the routing system
*/
class ExportForm extends \Drupal\Core\Form\FormBase {
private $default_content_manager;
private $entity;
/**
* Constructs a ContentEntityForm object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
*/
public function __construct($entity_type_manager, $route_match, $default_content_manager) {
$params = $route_match->getParameters()->all();
list($entity_type_id, $id) = each($params);
$this->entity = $entity_type_manager->getStorage($entity_type_id)->load($id);
$this->defaultContentManager = $default_content_manager;
}
static function create(\Symfony\Component\DependencyInjection\ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
$container->get('current_route_match'),
$container->get('default_content.manager')
);
}
function getFormId() {
return 'default_content_export_form';
}
function buildForm(array $form, FormStateInterface $form_state) {
$form['output'] = [
'#title' => $this->t('Output'),
'#type' => 'radios',
'#options' => [
'download' => $this->t('Download'),
'screen' => $this->t('Print to screen')
],
'#required' => TRUE,
];
$form['relations'] = [
'#title' => $this->t('Include relations'),
'#type' => 'checkbox',
'#states' => [
'visible' => [
':input[name=output]' => ['value' => 'screen']
]
]
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Export'),
'#submit' => [[$this, 'submitForm']]
];
if ($output = $form_state->get('exported')) {
foreach ($output as $entity_type_id => $entities) {
foreach ($entities as $uuid => $entity) {
$form['exported'] = [
'#title' => $entity_type_id,
'#type' => 'textarea',
'#description' => $this->t('Put this code in a file, in @path', ['@path' => 'MY_MODULE/content/'.$entity_type_id.'/'.$uuid.'.json']),
'#value' => $entity,
'#input' => FALSE
];
}
}
}
return $form;
}
protected function getEditedFieldNames(FormStateInterface $form_state) {
$fieldnames = getEditedFieldNames($form_state);
$fieldnames[] = 'output';
$fieldnames[] = 'relations';
return $fieldnames;
}
//this is being called statically, for some reason
function submitForm(array &$form, FormStateInterface $form_state) {
$params = \Drupal::routeMatch()->getParameters()->all();
list($entity_type_id, $id) = each($params);
if ($form_state->getValue('relations')) {
$entities = $this->defaultContentManager
->exportContentWithReferences($entity_type_id, $id);
}
else {
$entity = \Drupal::entityTypeManager()->getStorage($entity_type_id)->load($id);
$json = $this->defaultContentManager->exportContent($entity_type_id, $id);
$entities = [
$entity_type_id => [
$entity->uuid() => $json
]
];
}
if ($form_state->getValue('output') == 'download') {
if (!isset($entity)) {
drupal_set_message('creating of the zip file is not yet implemented');//@todo
}
$headers = [
'Content-Type' => 'application/octet-stream',
'Content-Length' => strlen($content),
'Content-Disposition' => 'attachment; filename='.$entity->uuid().'.json'
];
$content = $entities[$entity_type_id][$entity->uuid()];
$response = new Response($content, 200, $headers);
$response->send();
exit;
}
else {
$form_state->set('exported', $entities);
$form_state->setRebuild(TRUE);
}
}
}
<?php
/**
* @file
* Contains \Drupal\default_content\Plugin\Derivative\LocalTasks.
*/
namespace Drupal\default_content;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Provides local task definitions one to export each entity type
*/
class LocalTasks extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
var $entityTypeManager;
/**
* {@inheritdoc}
*/
public function __construct($entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
$this->derivatives = [];
foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $definition) {
if ($definition instanceOf \Drupal\Core\Entity\ContentEntityType) {
$this->derivatives["default_content.export.".$entity_type_id] = [
'route_name' => "entity.$entity_type_id.export",
'weight' => 5,
'title' => $this->t('Export'),
'base_route' => "entity.$entity_type_id.canonical",
];
}
}
return $this->derivatives;
}
}
<?php
/**
* @file
* Contains \Drupal\default_content\RouteSubscriber.
* Makes a new export route for every contentEntity
*/
namespace Drupal\default_content;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\Routing\Route;
/**
* Subscriber for one route to export each entity type
*/
class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
protected function alterRoutes(RouteCollection $collection) {
foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type_id => $definition) {
if ($definition instanceOf \Drupal\Core\Entity\ContentEntityType) {
$route = new Route($definition->getLinkTemplate('canonical') . '/export');
$route->setDefaults([
'_form' => '\Drupal\default_content\ExportForm',
'_title' => 'Export'
]);
$route->setRequirements([
'_permission' => 'access site reports',
]);
$collection->add('entity.'.$entity_type_id.'.export', $route);
}
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
RoutingEvents::ALTER => ['onAlterRoutes', 100]
];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment