You are here

protected function PullForm::buildEntitiesSelectTable in Entity Share 8

Same name and namespace in other branches
  1. 8.3 modules/entity_share_client/src/Form/PullForm.php \Drupal\entity_share_client\Form\PullForm::buildEntitiesSelectTable()
  2. 8.2 modules/entity_share_client/src/Form/PullForm.php \Drupal\entity_share_client\Form\PullForm::buildEntitiesSelectTable()

Helper function to generate entities table.

Parameters

array $form: The form array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

1 call to PullForm::buildEntitiesSelectTable()
PullForm::buildChannelSelect in modules/entity_share_client/src/Form/PullForm.php
Helper function to generate channel select.

File

modules/entity_share_client/src/Form/PullForm.php, line 376

Class

PullForm
Form controller to pull entities.

Namespace

Drupal\entity_share_client\Form

Code

protected function buildEntitiesSelectTable(array &$form, FormStateInterface $form_state) {
  $selected_remote = $form_state
    ->getValue('remote');
  $selected_channel = $form_state
    ->getValue('channel');
  $offset = '0';

  // No remote selected.
  if (empty($selected_remote) || empty($selected_channel) || !isset($this->channelsInfos[$selected_channel])) {
    $triggering_element = $form_state
      ->getTriggeringElement();
    $get_remote = $this->query
      ->get('remote');
    $get_channel = $this->query
      ->get('channel');
    $get_offset = $this->query
      ->get('offset');

    // If it is not an ajax trigger, check if it is in the GET parameters.
    if (!is_array($triggering_element) && !is_null($get_remote) && isset($this->remoteWebsites[$get_remote]) && !is_null($get_channel) && isset($this->channelsInfos[$get_channel])) {
      $selected_remote = $get_remote;
      $selected_channel = $get_channel;
      if (!is_null($get_offset) && is_int((int) $get_offset)) {
        $offset = $get_offset;
      }
    }
    else {
      return;
    }
  }
  $selected_remote = $this->remoteWebsites[$selected_remote];
  $http_client = $this->remoteManager
    ->prepareJsonApiClient($selected_remote);

  // Add offset to the selected channel.
  $parsed_url = UrlHelper::parse($this->channelsInfos[$selected_channel]['url']);
  $parsed_url['query']['page']['offset'] = $offset;
  $query = UrlHelper::buildQuery($parsed_url['query']);
  $prepared_url = $parsed_url['path'] . '?' . $query;
  $json_response = $http_client
    ->get($prepared_url)
    ->getBody()
    ->getContents();
  $json = Json::decode($json_response);

  // Store the JSONAPI links to use its in the pager submit handlers.
  $storage = $form_state
    ->getStorage();
  $storage['links'] = $json['links'];
  $form_state
    ->setStorage($storage);

  // Pager.
  $form['channel_wrapper']['entities_wrapper']['pager'] = [
    '#type' => 'actions',
    '#weight' => -10,
  ];
  if (isset($json['links']['first'])) {
    $form['channel_wrapper']['entities_wrapper']['pager']['first'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('First'),
      '#submit' => [
        '::firstPage',
      ],
    ];
  }
  if (isset($json['links']['prev'])) {
    $form['channel_wrapper']['entities_wrapper']['pager']['prev'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Previous'),
      '#submit' => [
        '::prevPage',
      ],
    ];
  }
  if (isset($json['links']['next'])) {
    $form['channel_wrapper']['entities_wrapper']['pager']['next'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Next'),
      '#submit' => [
        '::nextPage',
      ],
    ];
  }
  if (isset($json['links']['last'])) {
    $form['channel_wrapper']['entities_wrapper']['pager']['last'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Last'),
      '#submit' => [
        '::lastPage',
      ],
    ];
  }
  $form['channel_wrapper']['entities_wrapper']['actions_top']['#type'] = 'actions';
  $form['channel_wrapper']['entities_wrapper']['actions_top']['#weight'] = -1;
  $form['channel_wrapper']['entities_wrapper']['actions_top']['synchronize'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Synchronize entities'),
    '#button_type' => 'primary',
    '#validate' => [
      '::validateSelectedEntities',
    ],
    '#submit' => [
      '::synchronizeSelectedEntities',
    ],
  ];

  // Table to select entities.
  $header = [
    'label' => $this
      ->t('Label'),
    'type' => $this
      ->t('Type'),
    'bundle' => $this
      ->t('Bundle'),
    'language' => $this
      ->t('Language'),
    'status' => $this
      ->t('Status'),
  ];
  $form['channel_wrapper']['entities_wrapper']['entities'] = [
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $this->jsonapiHelper
      ->buildEntitiesOptions($json['data']),
    '#empty' => $this
      ->t('No entities to be pulled have been found.'),
    '#attached' => [
      'library' => [
        'entity_share_client/admin',
      ],
    ],
  ];
  $form['channel_wrapper']['entities_wrapper']['actions_bottom']['#type'] = 'actions';
  $form['channel_wrapper']['entities_wrapper']['actions_bottom']['synchronize'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Synchronize entities'),
    '#button_type' => 'primary',
    '#validate' => [
      '::validateSelectedEntities',
    ],
    '#submit' => [
      '::synchronizeSelectedEntities',
    ],
  ];
}