You are here

class GathercontentContentImportSelectForm in GatherContent 8

Class GathercontentContentImportSelectForm.

@package Drupal\gathercontent\Form

Hierarchy

Expanded class hierarchy of GathercontentContentImportSelectForm

1 string reference to 'GathercontentContentImportSelectForm'
gathercontent.routing.yml in ./gathercontent.routing.yml
gathercontent.routing.yml

File

src/Form/GathercontentContentImportSelectForm.php, line 14

Namespace

Drupal\gathercontent\Form
View source
class GathercontentContentImportSelectForm extends GathercontentMultistepFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'gathercontent_content_import_form';
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->store
      ->set('project_id', $form_state
      ->getValue('project'));
    $this->store
      ->set('nodes', array_filter($form_state
      ->getValue('content')));
    $this->store
      ->set('menu', array_intersect_key(array_filter($form_state
      ->getValue('menu')), array_filter($form_state
      ->getValue('content'))));
    $form_state
      ->setRedirect('gathercontent.import_confirm_form');
  }
  public function getContentTable(array &$form, FormStateInterface $form_state) {
    $form_state
      ->setRebuild(TRUE);
    return $form['import'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
GathercontentContentImportSelectForm::buildForm public function Form constructor. Overrides GathercontentMultistepFormBase::buildForm
GathercontentContentImportSelectForm::getContentTable public function
GathercontentContentImportSelectForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
GathercontentContentImportSelectForm::submitForm public function Form submission handler. Overrides GathercontentMultistepFormBase::submitForm
GathercontentMultistepFormBase::$date_formatter protected property Drupal\Core\Datetime\DateFormatter definition.
GathercontentMultistepFormBase::$entityQuery protected property
GathercontentMultistepFormBase::$entity_manager protected property Drupal\Core\Entity\EntityManager definition.
GathercontentMultistepFormBase::$store protected property
GathercontentMultistepFormBase::$tempStoreFactory protected property
GathercontentMultistepFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create
GathercontentMultistepFormBase::deleteStore protected function Helper method that removes all the keys from the store collection used for the multistep form.
GathercontentMultistepFormBase::saveData protected function Saves the data from the multistep form.
GathercontentMultistepFormBase::__construct public function
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.