You are here

public function CloneContentTypeForm::buildForm in Content 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/CloneContentTypeForm.php, line 30

Class

CloneContentTypeForm
Class CloneContentTypeForm.

Namespace

Drupal\content_type_clone\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  //Get the requested node type from url parameter.
  $nodeTypeName = \Drupal::request()->query
    ->get('node_type');

  //Load the node type.
  $entity = NodeType::load($nodeTypeName);

  //Source content type fieldset.
  $form['source'] = array(
    '#type' => 'details',
    '#title' => t('Content type source'),
    '#open' => FALSE,
  );

  //Source content type name.
  $form['source']['source_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Label'),
    '#required' => TRUE,
    '#default_value' => $entity
      ->label(),
    '#attributes' => array(
      'readonly' => 'readonly',
    ),
  );

  //Source content type machine name.
  $form['source']['source_machine_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Machine name'),
    '#required' => TRUE,
    '#default_value' => $entity
      ->id(),
    '#attributes' => array(
      'readonly' => 'readonly',
    ),
  );

  //Source content type description.
  $form['source']['source_description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#required' => FALSE,
    '#default_value' => $entity
      ->getDescription(),
    '#attributes' => array(
      'readonly' => 'readonly',
    ),
  );

  //Target content type fieldset.
  $form['target'] = array(
    '#type' => 'details',
    '#title' => t('Content type target'),
    '#open' => TRUE,
  );

  //Target content type name.
  $form['target']['target_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Label'),
    '#required' => TRUE,
  );

  //Target content type machine name.
  $form['target']['target_machine_name'] = array(
    '#type' => 'machine_name',
    '#title' => t('Machine name'),
    '#required' => TRUE,
    '#description' => $this
      ->t('A unique name for this item. It must only contain lowercase letters, numbers, and underscores.'),
  );

  //Target content type description
  $form['target']['target_description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#required' => FALSE,
  );

  //Copy nodes checkbox.
  $form['copy_source_nodes'] = array(
    '#type' => 'checkbox',
    '#title' => t('Copy all nodes from the source content type to the target content type'),
    '#required' => FALSE,
  );

  //Delete nodes checkbox.
  $form['delete_source_nodes'] = array(
    '#type' => 'checkbox',
    '#title' => t('Delete all nodes from the source content type after they have been copied to the target content type'),
    '#required' => FALSE,
  );

  //Token pattern fieldset.
  $form['patterns'] = array(
    '#type' => 'details',
    '#title' => t('Replacement patterns'),
    '#open' => FALSE,
  );

  //Display token options.
  if (\Drupal::moduleHandler()
    ->moduleExists('token')) {

    // Display the node title pattern field.
    $placeholder = t('Clone of @title', array(
      '@title' => '[node:title]',
    ));
    $form['patterns']['title_pattern'] = array(
      '#type' => 'textfield',
      '#title' => t('Node title pattern'),
      '#attributes' => array(
        'placeholder' => $placeholder,
      ),
    );
    $form['patterns']['token_tree'] = array(
      '#title' => t('Tokens'),
      '#theme' => 'token_tree_link',
      '#token_types' => array(
        'node',
      ),
      '#show_restricted' => TRUE,
      '#global_types' => TRUE,
      '#required' => TRUE,
    );
  }
  else {
    $form['patterns']['token_tree'] = array(
      '#markup' => '<p>' . t('Enable the <a href="@drupal-token">Token module</a> to view the available token browser.', array(
        '@drupal-token' => 'http://drupal.org/project/token',
      )) . '</p>',
    );
  }

  //Clone submit button.
  $form['cct_clone'] = array(
    '#type' => 'submit',
    '#value' => $this
      ->t('Clone content type'),
  );

  //Return the result.
  return $form;
}