You are here

public function GathercontentContentImportSelectForm::buildForm in GatherContent 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 GathercontentMultistepFormBase::buildForm

File

src/Form/GathercontentContentImportSelectForm.php, line 26

Class

GathercontentContentImportSelectForm
Class GathercontentContentImportSelectForm.

Namespace

Drupal\gathercontent\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $gc_module_path = drupal_get_path('module', 'gc');
  $form = parent::buildForm($form, $form_state);
  $created_mapping_ids = GathercontentMapping::loadMultiple();
  $projects = array();
  $mapping_array = array();
  foreach ($created_mapping_ids as $mapping) {

    /** @var $mapping GathercontentMapping */
    if ($mapping
      ->hasMapping()) {
      $projects[$mapping
        ->getGathercontentProjectId()] = $mapping
        ->getGathercontentProject();
      $mapping_array[$mapping
        ->getGathercontentTemplateId()] = array(
        'gc_template' => $mapping
          ->getGathercontentTemplate(),
        'ct' => $mapping
          ->getContentTypeName(),
      );
    }
  }
  $form['project'] = array(
    '#type' => 'select',
    '#title' => t('Select project'),
    '#options' => $projects,
    '#empty_option' => t('- Select -'),
    '#required' => TRUE,
    '#ajax' => array(
      'callback' => 'Drupal\\gathercontent\\Form\\GathercontentContentImportSelectForm::getContentTable',
      'wrapper' => 'edit-import',
      'method' => 'replace',
      'effect' => 'fade',
    ),
    '#default_value' => $this->store
      ->get('project_id') ? $this->store
      ->get('project_id') : 0,
    '#description' => t('You can only see projects with mapped templates in the dropdown.'),
  );
  $form['import'] = array(
    '#prefix' => '<div id="edit-import">',
    '#suffix' => '</div>',
    '#attached' => array(
      'library' => array(
        'gathercontent/filter',
      ),
    ),
  );
  $form['menu'] = array(
    '#type' => 'value',
  );
  if ($form_state
    ->hasValue('project') || $this->store
    ->get('project_id')) {
    $project_id = $form_state
      ->hasValue('project') ? $form_state
      ->getValue('project') : $this->store
      ->get('project_id');
    $content_obj = new Content();
    $content = $content_obj
      ->getContents($project_id);
    $content_table = array();
    foreach ($content as $item) {

      // If template is not empty, we have mapped template and item
      // isn't synced yet.
      if (!is_null($item->template_id) && $item->template_id != 'null' && isset($mapping_array[$item->template_id])) {

        // @FIXME
        // menu_parent_options() is gone in Drupal 8. To generate or work with menu trees, you'll need to
        // use the menu.link_tree service.
        //
        //
        // @see https://www.drupal.org/node/2226481
        // @see https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Menu%21MenuLinkTree.php/class/MenuLinkTree/8
        $content_table[$item->id] = array(
          'status' => array(
            'data' => '<div class="gc-item--status--color" style="background: ' . $item->status->data->color . ';"></div>' . $item->status->data->name,
            'class' => array(
              'gc-item',
              'status-item',
            ),
          ),
          'title' => array(
            'data' => $item->name,
            'class' => array(
              'gc-item',
              'gc-item--name',
            ),
          ),
          'updated' => array(
            'data' => date('F d, Y - H:i', strtotime($item->updated_at->date)),
            'class' => array(
              'gc-item',
              'gc-item-date',
            ),
            'data-date' => date('Y-m-d.H:i:s', strtotime($item->updated_at->date)),
          ),
          'template' => array(
            'data' => $mapping_array[$item->template_id]['gc_template'],
            'class' => array(
              'template-name-item',
            ),
          ),
        );
      }
    }
    $header = array(
      'status' => t('Status'),
      'title' => t('Item Name'),
      'updated' => t('Last updated in GatherContent'),
      'template' => t('GatherContent Template Name'),
    );
    $form['import']['content'] = array(
      '#type' => 'tableselect',
      '#header' => $header,
      '#options' => $content_table,
      '#empty' => t('No content available.'),
      //        '#filterwrapper' => array(
      //          'filter_wrapper' => array('gc-table--filter-wrapper', 'clearfix'),
      //          'counter_wrapper' => array('gc-table--counter', 'clearfix'),
      //        ),
      //        '#filterdescription' => t('You can only see items with mapped templates in the table.'),
      '#default_value' => $this->store
        ->get('nodes') ? $this->store
        ->get('nodes') : NULL,
    );

    // @FIXME
    // l() expects a Url object, created from a route name or external URI.
    // $form['import']['back_button'] = array(
    //       '#type' => 'markup',
    //       '#markup' => l(t('Back'), '/admin/config/gc/import', array('attributes' => array('class' => array('button')))),
    //     );
  }
  $form['actions']['submit']['#value'] = $this
    ->t('Next');
  return $form;
}