|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\os2web_rest_api\Form; |
| 4 | + |
| 5 | +use Drupal\Core\Config\ConfigFactoryInterface; |
| 6 | +use Drupal\Core\Extension\ModuleHandlerInterface; |
| 7 | +use Drupal\Core\Form\ConfigFormBase; |
| 8 | +use Drupal\Core\Form\FormStateInterface; |
| 9 | +use Drupal\Core\Link; |
| 10 | +use Drupal\Core\Url; |
| 11 | +use Drupal\Core\Render\Markup; |
| 12 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class SettingsForm. |
| 16 | + */ |
| 17 | +class SettingsForm extends ConfigFormBase { |
| 18 | + |
| 19 | + /** |
| 20 | + * The module handler service. |
| 21 | + * |
| 22 | + * @var \Drupal\Core\Extension\ModuleHandlerInterface |
| 23 | + */ |
| 24 | + protected $moduleHandler; |
| 25 | + |
| 26 | + /** |
| 27 | + * Constructs a \Drupal\system\ConfigFormBase object. |
| 28 | + * |
| 29 | + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory |
| 30 | + * The factory for configuration objects. |
| 31 | + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler |
| 32 | + * The module handler. |
| 33 | + */ |
| 34 | + public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) { |
| 35 | + parent::__construct($config_factory); |
| 36 | + $this->moduleHandler = $module_handler; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * {@inheritdoc} |
| 41 | + */ |
| 42 | + public static function create(ContainerInterface $container) { |
| 43 | + return new static( |
| 44 | + $container->get('config.factory'), |
| 45 | + $container->get('module_handler') |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * {@inheritdoc} |
| 51 | + */ |
| 52 | + protected function getEditableConfigNames() { |
| 53 | + return [ |
| 54 | + 'os2web_rest_api.settings', |
| 55 | + ]; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * {@inheritdoc} |
| 60 | + */ |
| 61 | + public function getFormId() { |
| 62 | + return 'os2web_reset_api_settings_form'; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Title callback. |
| 67 | + */ |
| 68 | + public static function getTitle() { |
| 69 | + return t('OS2Web Rest API Settings'); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * {@inheritdoc} |
| 74 | + */ |
| 75 | + public function buildForm(array $form, FormStateInterface $form_state) { |
| 76 | + $form['documentation_header'] = [ |
| 77 | + '#prefix' => '<h3>', |
| 78 | + '#markup' => $this->t('Documentation'), |
| 79 | + '#suffix' => '</h3>', |
| 80 | + ]; |
| 81 | + |
| 82 | + $form['documentation']= [ |
| 83 | + '#prefix' => '<p>', |
| 84 | + '#markup' => $this->t('See original documentation for this module and <a href="https://github.com/os2web/os2web_rest_api#how-does-it-work" target="_blank">how does it work</a>.'), |
| 85 | + '#suffix' => '</p>', |
| 86 | + ]; |
| 87 | + |
| 88 | + |
| 89 | + $path_array = [ |
| 90 | + 'OS2WEB Rest list node' => '/admin/structure/views/view/os2web_rest_list_node', |
| 91 | + 'OS2WEB Rest list term' => '/admin/structure/views/view/os2web_rest_list_term', |
| 92 | + ]; |
| 93 | + $predefined_url = [ |
| 94 | + '#theme' => 'item_list', |
| 95 | + '#items' => [], |
| 96 | + ]; |
| 97 | + |
| 98 | + foreach ($path_array as $label => $path) { |
| 99 | + $predefined_url['#items'][$label] = Link::fromTextAndUrl($label, Url::fromUri('internal:' . $path, ['absolute' => TRUE]))->toString(); |
| 100 | + } |
| 101 | + $form['urls']= [ |
| 102 | + '#prefix' => '<p>', |
| 103 | + '#markup' => $this->t('Predefined views to export list of entities:@list', [ |
| 104 | + '@list' => Markup::create(\Drupal::service('renderer')->renderPlain($predefined_url)), |
| 105 | + ]), |
| 106 | + '#suffix' => '</p>', |
| 107 | + ]; |
| 108 | + |
| 109 | + $form['auth_header'] = [ |
| 110 | + '#prefix' => '<h3>', |
| 111 | + '#markup' => $this->t('Authorization'), |
| 112 | + '#suffix' => '</h3>', |
| 113 | + ]; |
| 114 | + |
| 115 | + if ($this->moduleHandler->moduleExists('basic_auth')) { |
| 116 | + $form['basic_auth'] = [ |
| 117 | + '#prefix' => '<p>', |
| 118 | + '#markup' => $this->t('You can configure basic authorization for listing by editing access section in views @node_list_api_url or @term_list_api_url.', [ |
| 119 | + '@node_list_api_url' => $predefined_url['#items']['OS2WEB Rest list node'], |
| 120 | + '@term_list_api_url' => $predefined_url['#items']['OS2WEB Rest list term'], |
| 121 | + ]), |
| 122 | + '#suffix' => '</p>', |
| 123 | + ]; |
| 124 | + } |
| 125 | + else { |
| 126 | + $form['basic_auth'] = [ |
| 127 | + '#prefix' => '<p>', |
| 128 | + '#markup' => $this->t('To configure basic authorization download and activate <a href="https://www.drupal.org/project/basic_auth" target="_blank">basic_auth</a> module.'), |
| 129 | + '#suffix' => '</p>', |
| 130 | + ]; |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + $form['extensions_header'] = [ |
| 135 | + '#prefix' => '<h3>', |
| 136 | + '#markup' => $this->t('Useful extensions'), |
| 137 | + '#suffix' => '</h3>', |
| 138 | + ]; |
| 139 | + |
| 140 | + if ($this->moduleHandler->moduleExists('restui')) { |
| 141 | + $form['restui'] = [ |
| 142 | + '#prefix' => '<p>', |
| 143 | + '#markup' => $this->t('Get <a href="/admin/config/services/rest">overview on RESTfull API configuration</a>'), |
| 144 | + '#suffix' => '</p>', |
| 145 | + ]; |
| 146 | + } |
| 147 | + else { |
| 148 | + $form['restui'] = [ |
| 149 | + '#prefix' => '<p>', |
| 150 | + '#markup' => $this->t('To get overview on RESTfull API configuration you need to download and activate <a href="https://www.drupal.org/project/restui" target="_blank">restui</a> module.'), |
| 151 | + '#suffix' => '</p>', |
| 152 | + ]; |
| 153 | + } |
| 154 | + // There is nothing to save. |
| 155 | + // Skipping parent::buildForm($form, $form_state) call. |
| 156 | + return $form; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * {@inheritdoc} |
| 161 | + */ |
| 162 | + public function submitForm(array &$form, FormStateInterface $form_state) { |
| 163 | + // There is nothing to save here at the current moment. |
| 164 | + parent::submitForm($form, $form_state); |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments