public function CloneEntityType::buildForm in Entity Type Clone 8
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides FormInterface::buildForm
File
- src/
Form/ CloneEntityType.php, line 45
Class
- CloneEntityType
- Class CloneEntityType.
Namespace
Drupal\entity_type_clone\FormCode
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';
// Create the part of the form that allows the user to select the basic
// properties of what the entity to delete.
$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();
/* @var $definition \Drupal\Core\Entity\EntityTypeInterface */
$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 the current base table support bundles and has more than one (like user).
if (!empty($default_bundles)) {
// Get all bundles and their human readable names.
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>',
];
}
// Target content type fieldset.
$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;
}