You are here

public function ConfigFieldsExportToCodeForm::buildForm in Field tools 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/ConfigFieldsExportToCodeForm.php, line 75

Class

ConfigFieldsExportToCodeForm
Provides a form to clone multiple fields from an entity bundle.

Namespace

Drupal\field_tools\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $bundle = NULL) {
  $this->entityTypeId = $entity_type_id;
  $this->bundle = $bundle;
  $field_manager = \Drupal::service('entity_field.manager');
  $field_definitions = $field_manager
    ->getFieldDefinitions($entity_type_id, $bundle);
  $code = [];
  if (!empty($form_state
    ->getValues())) {
    $code = [];
    foreach (array_filter($form_state
      ->getValues()['fields']) as $field_name) {
      $this
        ->getBaseFieldCode($code, $field_definitions[$field_name]);

      // TODO: bundle fields and config field conversion.
      $code[] = '';
    }
    $form['code'] = [
      '#type' => 'textarea',
      '#value' => implode("\n", $code),
      '#rows' => count($code) + 1,
    ];
  }
  $field_options = [];
  foreach ($field_definitions as $field_id => $field) {
    $field_options[$field_id] = $this
      ->t("@field-label (machine name: @field-name)", array(
      '@field-label' => $field
        ->getLabel(),
      '@field-name' => $field
        ->getName(),
    ));
  }
  asort($field_options);
  $form['fields'] = array(
    '#title' => $this
      ->t('Fields to export to code'),
    '#type' => 'checkboxes',
    '#options' => $field_options,
    '#description' => $this
      ->t("Select fields to export as entity class code."),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => $this
      ->t('Export field definitions to code'),
  );
  return $form;
}