You are here

public function ShrinkTheWebSettingsForm::_submitForm in ShrinkTheWeb 8

Realize form checkbox handlers.

Parameters

array $form:

\Drupal\Core\Form\FormStateInterface $form_state:

1 call to ShrinkTheWebSettingsForm::_submitForm()
ShrinkTheWebSettingsForm::submitForm in src/Form/ShrinkTheWebSettingsForm.php
Form submission handler.

File

src/Form/ShrinkTheWebSettingsForm.php, line 452

Class

ShrinkTheWebSettingsForm

Namespace

Drupal\shrinktheweb\Form

Code

public function _submitForm(array &$form, FormStateInterface $form_state) {
  module_load_include('inc', 'shrinktheweb', 'shrinktheweb.api');
  $clear_imagecache = $form_state
    ->getValue('shrinktheweb_clear_imagecache');
  if (!empty($clear_imagecache)) {
    if ($clear_imagecache == TRUE) {
      shrinktheweb_deleteAllImages();
      drupal_set_message(t('All cached thumbshots got deleted'));
    }
  }
  $clear_errorcache = $form_state
    ->getValue('shrinktheweb_clear_errorcache');
  if (!empty($clear_errorcache)) {
    if ($clear_errorcache == TRUE) {
      shrinktheweb_deleteErrorImages();
      drupal_set_message(t('All cached error images got deleted'));
    }
  }
  $refresh_thumbnails = $form_state
    ->getValue('shrinktheweb_refresh_thumbnails');
  if (!empty($refresh_thumbnails)) {
    if ($refresh_thumbnails == TRUE) {
      $shrinktheweb_fields = \Drupal::database()
        ->select('shrinktheweb_fields', 't')
        ->fields('t', array(
        'stw_entity_bundle',
        'stw_field_name',
      ))
        ->execute();
      $fields = array();
      $field = $shrinktheweb_fields
        ->fetchAssoc();
      while ($field) {
        $fields = array_merge_recursive($fields, array(
          $field['stw_entity_bundle'] => $field['stw_field_name'],
        ));
        $field = $shrinktheweb_fields
          ->fetchAssoc();
      }
      $urls = array();
      foreach ($fields as $bundle => $field_list) {
        $nids = \Drupal::entityQuery('node')
          ->condition('type', $bundle)
          ->execute();
        $nodes = \Drupal::entityTypeManager()
          ->getStorage('node')
          ->loadMultiple($nids);
        foreach ($nodes as $node) {
          if (is_array($field_list)) {
            foreach ($field_list as $field) {
              $node_array = $node
                ->toArray();
              if (!empty($node_array[$field]['0']['uri'])) {
                $urls[] = $node_array[$field]['0']['uri'];
              }
            }
          }
          elseif (!empty($field_list)) {
            $node_array = $node
              ->toArray();
            $urls[] = $node_array[$field_list]['0']['uri'];
          }
        }
      }
      foreach ($urls as $url) {
        $operations_request[] = array(
          'requestThumbnailsRefresh',
          array(
            $url,
          ),
        );
      }
      if (count($operations_request) > 0) {
        $batch = array(
          'operations' => $operations_request,
          'finished' => 'requestThumbnailsRefreshFinished',
          'title' => t('Requesting thumbnails refresh'),
          'init_message' => t('Preparing to request thumbnails refresh'),
          'progress_message' => t('Requested refresh for @current of @total thumbnails.'),
          'error_message' => t('An error has occurred.'),
          'file' => drupal_get_path('module', 'shrinktheweb') . '/src/RefreshThumbnailsBatch.inc',
        );
        batch_set($batch);
      }
    }
  }
}