function protected function CdnProvider::importProviderData

8.x-3.x CdnProvider.php protected CdnProvider::importProviderData(Element $group, FormStateInterface $form_state)

Imports data for a provider that was manually uploaded in theme settings.

@todo Import functionality is deprecated, remove in a future release.

Parameters

\Drupal\bootstrap\Utility\Element $group: The setting group Element.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Class

CdnProvider
The "cdn_provider" theme setting.

Namespace

Drupal\bootstrap\Plugin\Setting\Advanced\Cdn

Source src/Plugin/Setting/Advanced/Cdn/CdnProvider.php (line 218)

protected function importProviderData(Element $group, FormStateInterface $form_state) {
  if ($form_state->getValue('clicked_button') === t('Save provider data')->render()) {
    $provider_path = ProviderManager::FILE_PATH;

    // FILE_CREATE_DIRECTORY = 1 | FILE_MODIFY_PERMISSIONS = 2.
    $options = 1 | 2;
    if ($fileSystem = Bootstrap::fileSystem('prepareDirectory')) {
      $fileSystem->prepareDirectory($provider_path, $options);
    }
    else {
      file_prepare_directory($provider_path, $options);
    }

    $provider = $form_state->getValue('cdn_provider', $this->theme->getSetting('cdn_provider'));
    $file = "$provider_path/$provider.json";

    if ($import_data = $form_state->getValue('cdn_provider_import_data', FALSE)) {
      // FILE_EXISTS_REPLACE = 1.
      $replace = 1;
      if ($fileSystem = Bootstrap::fileSystem('saveData')) {
        $fileSystem->saveData($import_data, $file, $replace);
      }
      else {
        file_unmanaged_save_data($import_data, $file, $replace);
      }
    }
    elseif ($file && file_exists($file)) {
      if ($fileSystem = Bootstrap::fileSystem('delete')) {
        $fileSystem->delete($file);
      }
      else {
        file_unmanaged_delete($file);
      }
    }

    // Clear the cached definitions so they can get rebuilt.
    $providerManager = new ProviderManager($this->theme);
    $providerManager->clearCachedDefinitions();
    $form_state->setRebuild();
    return;
  }

  $provider = $this->getProvider();
  $plugin_id = Html::cleanCssIdentifier($provider->getPluginId());

  // To avoid triggering unnecessary deprecation messages, extract these
  // values from the provider definition directly.
  // @todo Import functionality is deprecated, remove in a future release.
  $definition = $provider->getPluginDefinition();
  $hasError = !empty($definition['error']);
  $isImported = !empty($definition['imported']);

  // Indicate there was an error retrieving the provider's API data.
  if ($hasError || $isImported) {
    if ($isImported) {
      Bootstrap::deprecated('\Drupal\bootstrap\Plugin\Provider\ProviderInterface::isImported');
    }
    if ($hasError) {
      // Now a deprecation message can be shown as the provider clearly is
      // using the outdated "process definition" method of providing assets.
      Bootstrap::deprecated('\Drupal\bootstrap\Plugin\Provider\ProviderInterface::hasError');
      $description_label = $this->t('ERROR');
      $description = $this->t('Unable to reach or parse the data provided by the @title API. Ensure the server this website is hosted on is able to initiate HTTP requests. If the request consistently fails, it is likely that there are certain PHP functions that have been disabled by the hosting provider for security reasons. It is possible to manually copy and paste the contents of the following URL into the "Imported @title data" section below.<br /><br /><a href=":provider_api" target="_blank">:provider_api</a>.', [
        '@title' => $provider->getLabel(),
        ':provider_api' => $provider->getApi(),
      ]);
      $group->error = [
        '#markup' => '<div class="alert alert-danger messages error"><strong>' . $description_label . ':</strong> ' . $description . '</div>',
        '#weight' => -20,
      ];
    }

    $group->import = [
      '#type' => 'details',
      '#title' => t('Imported @title data', ['@title' => $provider->getLabel()]),
      '#description' => t('The provider will attempt to parse the data entered here each time it is saved. If no data has been entered, any saved files associated with this provider will be removed and the provider will again attempt to request the API data normally through the following URL: <a href=":provider_api" target="_blank">:provider_api</a>.', [
        ':provider_api' => $provider->getPluginDefinition()['api'],
      ]),
      '#weight' => 10,
      '#open' => FALSE,
    ];

    $group->import->cdn_provider_import_data = [
      '#type' => 'textarea',
      '#default_value' => file_exists(ProviderManager::FILE_PATH . '/' . $plugin_id . '.json') ? file_get_contents(ProviderManager::FILE_PATH . '/' . $plugin_id . '.json') : NULL,
    ];

    $group->import->submit = $this->setCdnProvidersAjax([
      '#type' => 'submit',
      '#value' => t('Save provider data'),
      '#executes_submit_callback' => FALSE,
    ]);
  }
}