View source
<?php
namespace Drupal\entity_type_clone\Form;
use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CloneEntityType extends FormBase {
public function getFormId() {
return 'entity_type_clone_form';
}
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$params = \Drupal::request()->query;
$disbaled = FALSE;
if ($params) {
$entity_type = $params
->get('entity');
$bundle_type = $params
->get('bundle');
if ($entity_type && $bundle_type) {
$disbaled = TRUE;
}
}
$form['displays'] = [];
$input =& $form_state
->getUserInput();
$wrapper = 'entity-wrapper';
$form['displays']['show'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Entity Clone Settings'),
'#tree' => TRUE,
'#attributes' => [
'class' => [
'container-inline',
],
],
];
$content_entity_types = [];
$entity_type_definations = $this->entityTypeManager
->getDefinitions();
$clone_types = [
'node',
'paragraph',
'taxonomy_term',
'profile',
];
foreach ($entity_type_definations as $definition) {
if ($definition instanceof ContentEntityType) {
if (in_array($definition
->id(), $clone_types)) {
$content_entity_types[$definition
->id()] = $definition
->getLabel();
}
}
}
$form['displays']['show']['entity_type'] = [
'#type' => 'select',
'#title' => $this
->t('Select Entity Type'),
'#options' => $content_entity_types,
'#empty_option' => $this
->t('- Select Entity Type -'),
'#size' => 1,
'#required' => TRUE,
'#disabled' => $disbaled,
'#default_value' => isset($entity_type) ? $entity_type : '',
'#suffix' => '<div id="' . $wrapper . '"></div>',
'#ajax' => [
'callback' => [
$this,
'ajaxCallChangeEntity',
],
'wrapper' => $wrapper,
],
];
if (isset($input['show']['entity_type']) || isset($entity_type)) {
$entity_type_selected = isset($input['show']['entity_type']) ? $input['show']['entity_type'] : $entity_type;
$default_bundles = \Drupal::service('entity_type.bundle.info')
->getBundleInfo($entity_type_selected);
if (!empty($default_bundles)) {
foreach ($default_bundles as $type => $bundle) {
$type_options[$type] = $bundle['label'];
}
$form['displays']['show']['type']['#options'] = $type_options;
}
}
if (isset($type_options)) {
$form['displays']['show']['type'] = [
'#type' => 'select',
'#title' => $this
->t('of type'),
'#options' => $type_options,
'#disabled' => $disbaled,
'#default_value' => $bundle_type,
'#prefix' => '<div id="' . $wrapper . '">',
'#suffix' => '</div>',
];
}
$form['target'] = [
'#type' => 'details',
'#title' => $this
->t('Target Entity details'),
'#open' => TRUE,
];
$form['target']['clone_bundle'] = [
'#type' => 'textfield',
'#title' => $this
->t('Target bundle name'),
'#required' => TRUE,
];
$form['target']['clone_bundle_machine'] = [
'#type' => 'textfield',
'#title' => $this
->t('Target bundle machine name'),
'#required' => TRUE,
];
$form['target']['target_description'] = [
'#type' => 'textarea',
'#title' => $this
->t('Description'),
'#required' => FALSE,
];
$form['message'] = [
'#markup' => $this
->t('Note: Use <b>ENTITY TYPE CLONE</b> only to clone Content Type, Paragraph, Taxonomy.<br>'),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Clone'),
];
$form['reset'] = [
'#type' => 'submit',
'#value' => $this
->t('Reset'),
];
return $form;
}
public function ajaxCallChangeEntity(array &$form, FormStateInterface $form_state) {
return $form['displays']['show']['type'];
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$entity_type = $values['show']['entity_type'];
$entityTypesNames = $this
->getMachineNamesof($entity_type);
if ($entityTypesNames && in_array($values['clone_bundle_machine'], $entityTypesNames)) {
$form_state
->setErrorByName('clone_bundle_machine', $this
->t('The machine name of the target entity type already exists.'));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$op = (string) $values['op'];
if ($op == $this
->t('Reset')) {
$form_state
->setRedirect('entity_type_clone.type');
}
elseif ($op == $this
->t('Clone')) {
$batch = [
'title' => $this
->t('Cloning in process.'),
'operations' => $this
->cloneEntityType($form_state),
'init_message' => $this
->t('Performing clone operations...'),
'finished' => '\\Drupal\\entity_type_clone\\Form\\CloneEntityTypeData::cloneEntityTypeFinishedCallback',
'error_message' => $this
->t('Something went wrong. Please check the errors log.'),
];
batch_set($batch);
}
}
public function cloneEntityType(FormStateInterface $form_state) {
$values = $form_state
->getValues();
$entity_type = $values['show']['entity_type'];
$operations = [];
$operations[] = [
'\\Drupal\\entity_type_clone\\Form\\CloneEntityTypeData::cloneEntityTypeData',
[
$values,
],
];
$fields = \Drupal::service('entity_field.manager')
->getFieldDefinitions($entity_type, $values['show']['type']);
foreach ($fields as $field) {
if (!empty($field
->getTargetBundle()) && !($entity_type === 'taxonomy_term' && $field
->getName() === 'parent')) {
$data = [
'field' => $field,
'values' => $values,
];
$operations[] = [
'\\Drupal\\entity_type_clone\\Form\\CloneEntityTypeData::cloneEntityTypeField',
[
$data,
],
];
}
}
return $operations;
}
protected function getMachineNamesof($entity_type) {
$entityTypesNames = [];
if ($entity_type === 'node') {
$contentTypes = \Drupal::service('entity_type.manager')
->getStorage('node_type')
->loadMultiple();
$entityTypesNames = [];
foreach ($contentTypes as $contentType) {
$entityTypesNames[] = $contentType
->id();
}
}
elseif ($entity_type === 'taxonomy_term') {
$taxonomyTypes = taxonomy_vocabulary_get_names();
foreach ($taxonomyTypes as $taxonomyType) {
$entityTypesNames[] = $taxonomyType;
}
}
return $entityTypesNames;
}
}