funder_field-1.1.0-alpha1/src/Form/FunderFieldSettingsForm.php

src/Form/FunderFieldSettingsForm.php
<?php

namespace Drupal\funder_field\Form;

use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Menu\MenuLinkManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class FunderFieldSettingsForm extends ConfigFormBase {
  /**
   * The cache menu instance.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cacheMenu;

  /**
   * The menu link manager instance.
   *
   * @var \Drupal\Core\Menu\MenuLinkManagerInterface
   */
  protected $menuLinkManager;

  /**
   * FunderFieldSettingsForm constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory for the form.
   * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menuLinkManager
   *   A menu link manager instance.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cacheMenu
   *   A cache menu instance.
   */
  public function __construct(ConfigFactoryInterface $config_factory, MenuLinkManagerInterface $menuLinkManager, CacheBackendInterface $cacheMenu) {
    parent::__construct($config_factory);
    $this->cacheMenu = $cacheMenu;
    $this->menuLinkManager = $menuLinkManager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory'),
      $container->get('plugin.manager.menu.link'),
      $container->get('cache.menu')
    );
  }

  /**
   * {@inheritDoc}
   */
  protected function getEditableConfigNames() {
    return [
      'funder_field.settings',
    ];
  }

  /**
   * {@inheritDoc}
   */
  public function getFormId() {
    return 'funder_field_settings';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('funder_field.settings');
    $depth_values = range(1, 20);

    // Choose how many items will be shown in the drop-down list
    $form['funder_items_depth'] = [
      '#type' => 'select',
      '#title' => $this->t('No of funders to show in autocomplete'),
      '#description' => $this->t('Maximal items to display inside "funder Name" field.'),
      '#default_value' => $config->get('funder_items_depth'),
      '#options' => array_combine($depth_values, $depth_values),
    ];

    //Check whether API of Crossref Funder Registry running OK or not
    $form['markup'] = [
      '#markup' => $this->t('<b>Check API status of Crossref Funder Registry</b>'),
    ];

    $form['check_api_status'] = [
      '#type' => 'submit',
      '#value' => $this->t('Check API Status'),
      "#weight" => 1,
      '#submit' => array([$this, 'checkApiStatus']),
      '#limit_validation_errors' => array()
    ];

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function checkApiStatus(array &$form, FormStateInterface $form_state)
  {
    $response2 = \Drupal::httpClient()
      ->get('https://api.crossref.org/funders/100000001/');

    $statusCode = $response2->getStatusCode();

    if ($statusCode != 200) {
      \Drupal::messenger()->addError(t("
        <b>API service is down </b><br/>
        <b>Website: </b> <a href='https://www.crossref.org/documentation/funder-registry/funder-data-via-the-api/' target='_blank'>Research Funder Registry</a> <br/>
        "));
    }
    else {
      $json_string = $response2->getBody();

      \Drupal::messenger()->addMessage(t("
        <div>
          <b>API service is up and running </b><br/>
          <b>Website: </b> <a href='https://www.crossref.org/documentation/funder-registry/funder-data-via-the-api/' target='_blank'>Research Funder Registry</a> <br/>
          <b>Endpoint URL: </b>
          <a>https://api.crossref.org/funders/100000001/</a>
          <br/>
          <b>Status Code: </b> $statusCode <br/>
          <a href='#' type='button' onclick='document.getElementById(`content`).style.display = (document.getElementById(`content`).style.display == `none`) ? `block` : `none`' class='collapsible'><b>Show/Hide Response Body </b></a> <br/>

          <div id='content' style='display:none'>
            $json_string <br/>
          </div>
        </div>
        "));
    }

  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->config('funder_field.settings')
      ->set('funder_items_depth', $form_state->getValue('funder_items_depth'))
      ->save();
    parent::submitForm($form, $form_state);
    $this->cacheMenu->invalidateAll();
    $this->menuLinkManager->rebuild();
  }

}

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

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