You are here

public function ContentExportManagerForm::buildForm in Content Export YAML 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/ContentExportManagerForm.php, line 39

Class

ContentExportManagerForm
Class ConfigImportForm.

Namespace

Drupal\content_export_yaml\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $settings = $this
    ->config('content_export_yaml.contentexportsetting');
  $export = \Drupal::service('content_export_yaml.manager');
  $query = $this
    ->getRequest()->query
    ->all();
  $path = $settings
    ->get('path_export_content_folder');
  $form_state
    ->setMethod('GET');
  if (isset($query['file_single'])) {
    $result = DRUPAL_ROOT . $query['file_single'];
    $config_name = basename($result, '.yml');
    $bundle = $this
      ->_getBundleName($result);
    $entity_type = $this
      ->_getEntityType($result);
    $stat = $export
      ->import($config_name, $entity_type, $bundle);
    if ($stat) {
      \Drupal::messenger()
        ->addMessage(t("Imported Successfully"), 'status');
    }
    return new RedirectResponse(Url::fromRoute('content_export_yaml.manage_content_yaml_form')
      ->toString());
  }
  if (isset($query['delete'])) {
    $filepath = DRUPAL_ROOT . $query['delete'];
    $status = false;
    if (is_file($filepath)) {
      $status = unlink($filepath);
    }
    if ($status) {
      \Drupal::messenger()
        ->addMessage(t("File " . $query['delete'] . " deleted Successfully"), 'status');
    }
    return new RedirectResponse(Url::fromRoute('content_export_yaml.manage_content_yaml_form')
      ->toString());
  }
  if (isset($query['op']) && $query['op'] == 'Import all') {
    $path = $query['path'] ? $path . "/" . $query['path'] : $path;
    $config_path = DRUPAL_ROOT . $path;
    $results = $this
      ->_readDirectory($config_path, 'yml');
    $batch = [
      'title' => $this
        ->t('Import Content From yml...'),
      'operations' => [],
      'init_message' => $this
        ->t('Starting ..'),
      'progress_message' => $this
        ->t('Processd @current out of @total.'),
      'error_message' => $this
        ->t('An error occurred during processing.'),
      'finished' => '\\Drupal\\content_export_yaml\\Form\\ContentExportManagerForm::importFinishedCallback',
    ];
    if (isset($query['key']) && $query['key'] != '') {
      foreach ($results as $key => $result) {
        $config_name = basename($result, '.yml');
        $filter = $query['key'];
        if (is_string($filter) && strpos($config_name, $filter) !== false) {
          $batch['operations'][] = [
            $this
              ->importElement($result),
            [],
          ];
        }
      }
    }
    else {
      foreach ($results as $key => $result) {
        $batch['operations'][] = [
          $this
            ->importElement($result),
          [],
        ];
      }
    }
    batch_set($batch);
    return batch_process(Url::fromRoute('content_export_yaml.manage_content_yaml_form')
      ->toString());

    //return new RedirectResponse(Url::fromRoute('content_export_yaml.manage_content_yaml_form')->toString());
  }
  $form['key'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Search by file name'),
    '#attributes' => [
      'name' => 'key',
    ],
    '#default_value' => isset($query['key']) ? $query['key'] : '',
    '#description' => 'Make empty to get all',
  ];
  $form['path'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Folder path '),
    '#attributes' => [
      'name' => 'path',
    ],
    '#description' => 'if Empty show all , For example : /node ',
    '#default_value' => isset($query['path']) ? $query['path'] : '',
  ];
  $header = [
    'id' => t('Number'),
    'folder' => t('Folder path'),
    'file' => t('File name'),
    'status' => t('Status'),
    'operation' => t('Actions'),
  ];
  $output = [];
  if (isset($query['op']) && $query['op'] == 'Search' && isset($query['path']) && $query['path'] != '') {
    $key = '';
    $path = $path . "/" . $query['path'];
    if (isset($query['key'])) {
      $key = $query['key'];
    }
    $form['help'] = [
      '#type' => 'item',
      '#title' => t('Selected key and path'),
      '#markup' => 'Key:' . $key . '<br/> Path : ' . $query['path'],
    ];
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => 'Search',
  ];
  $filter = null;
  $config_path = DRUPAL_ROOT . $path;
  $results = $this
    ->_readDirectory($config_path, 'yml');
  $form['actions']['import'] = [
    '#type' => 'submit',
    '#value' => 'Import all',
  ];
  if (isset($query['key']) && $query['key'] != '') {
    foreach ($results as $key => $result) {
      $is_ok = $export
        ->isReadyToImport($result);
      if ($is_ok) {
        $status = Markup::create('<span style="color:blue">Clean </span>');
      }
      else {
        $status = Markup::create('<span style="color:red">Yaml Content has error</span>');
      }
      $config_name = basename($result, '.yml');
      $root_folder = dirname($result);
      $root_folder = str_replace(DRUPAL_ROOT, '', $root_folder);
      $filter = $query['key'];
      if (is_string($filter) && strpos($config_name, $filter) !== false) {
        $bundle = basename($root_folder);
        $entity_type = basename($bundle);
        $operations = $this
          ->_tableActions($root_folder, $config_name);
        $output[] = [
          'id' => $key + 1,
          'folder' => $root_folder,
          'file' => $config_name,
          'Status' => $status,
          'operation' => array(
            'data' => array(
              '#type' => 'operations',
              '#links' => $operations,
            ),
          ),
        ];
      }
    }
  }
  else {
    foreach ($results as $key => $result) {
      $is_ok = $export
        ->isReadyToImport($result);
      if ($is_ok) {
        $status = Markup::create('<span style="color:blue">Clean </span>');
      }
      else {
        $status = Markup::create('<span style="color:red">Yaml Content has error</span>');
      }
      $config_name = basename($result, '.yml');
      $root_folder = dirname($result);
      $root_folder = str_replace(DRUPAL_ROOT, '', $root_folder);
      $operations = $this
        ->_tableActions($root_folder, $config_name);
      $output[] = [
        'id' => $key + 1,
        'folder' => $root_folder,
        'file' => $config_name,
        'Status' => $status,
        'operation' => array(
          'data' => array(
            '#type' => 'operations',
            '#links' => $operations,
          ),
        ),
      ];
    }
  }
  $row_pagination = $this
    ->_return_pager_for_array($output, 25);
  $form['table'] = array(
    '#type' => 'table',
    '#weight' => 999,
    '#header' => $header,
    '#rows' => $row_pagination,
    '#empty' => $this
      ->t('No variables found'),
  );
  $form['pager'] = array(
    '#type' => 'pager',
    '#weight' => 999,
    '#quantity' => 5,
  );
  $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
  return $form;
}