archivesspace-8.x-1.x-dev/src/Form/BatchUpdateForm.php

src/Form/BatchUpdateForm.php
<?php

namespace Drupal\archivesspace\Form;

use Drupal\archivesspace\BatchUpdateBuilder;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use GuzzleHttp\Exception\ClientException;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Implments a Batch Update form.
 */
class BatchUpdateForm extends FormBase {

  /**
   * Batch Update Builder.
   *
   * @var \Drupal\archivesspace\BatchUpdateBuilder
   */
  protected $batchUpdateBuilder;

  /**
   * State stores last updated time.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * The logger factory.
   *
   * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
   */
  protected $logger;

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

  /**
   * Constructor.
   *
   * @param \Drupal\archivesspace\BatchUpdateBuilder $bub
   *   The class responsible for building batch updates for processing.
   * @param \Drupal\Core\Session\StateInterface $state
   *   State stores connection info.   *.
   */
  public function __construct(BatchUpdateBuilder $bub, StateInterface $state) {
    $this->batchUpdateBuilder = $bub;
    $this->state = $state;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('archivesspace.batch_update_builder'),
      $container->get('state')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    $form['#cache'] = ['max-age' => 0];

    try {
      $this->batchUpdateBuilder->getArchivesSpaceSession()->getSession();
    }
    catch (ClientException $e) {
      $this->logger('archivesspace')->warning('Could not connect to ArchivesSpace: ' . $e->getMessage());
      $form['warning'] = [
        '#type' => 'item',
        '#markup' => $this->t('Unable to connect to ArchivesSpace. Please check your <a href="/admin/archivesspace/config">ArchivesSpace Core Settings</a>!'),
      ];
      return $form;
    }

    $form['max_pages'] = [
      '#type' => 'number',
      '#title' => 'Max Pages',
      '#size' => 5,
      '#description' => $this->t('Maximum number of pages to process. Optional, leave blank to process all updates.'),
      '#required' => FALSE,
    ];

    // Updated since.
    $latest_user_mtime = $this->state->get('archivesspace.latest_user_mtime');
    $timestamp = (!empty($latest_user_mtime)) ? strtotime($latest_user_mtime) : 1;
    $form['updated_since'] = [
      '#type' => 'datetime',
      '#title' => 'Updated Since',
      '#default_value' => DrupalDateTime::createFromTimestamp($timestamp),
      '#description' => $this->t('When to start looking for updates. Auto-populated with the last known successful update timestamp.'),
      '#required' => TRUE,
    ];

    // Types dropdown?
    $form['submit_button'] = [
      '#type' => 'submit',
      '#value' => $this->t('Start Updates'),
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $max_pages = intval($form_state->getValue('max_pages'));
    if ($max_pages > 0) {
      $this->batchUpdateBuilder->setMaxPages($max_pages);
    }
    $this->batchUpdateBuilder->setUpdatedSince($form_state->getValue('updated_since')->format(DATE_ATOM));
    if ($batch = $this->batchUpdateBuilder->buildBatchDefinition()) {
      batch_set($batch);
    }
    else {
      $this->messenger()->addMessage($this->t("No updates to process!"));
    }

  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc