Skip to content

Commit

Permalink
Added settings page with information about module
Browse files Browse the repository at this point in the history
  • Loading branch information
andriyun committed Aug 27, 2021
1 parent 8793a5a commit f4e2dab
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 4 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,22 @@ that is included in Drupal core.
Module provides preconfigured REST sources and Views for fetching lists nodes
and terms in JSON-format
* /node/[node id]?_format=json - particular node
* /rest/os2web/list/term - list of taxonomy terms
* /rest/os2web/list/node/[taxonomy term id] - list of nodes filtered by
[taxonomy term id]
* /rest/os2web/list/term - list of all taxonomy terms
* /rest/os2web/list/term/[vocabulary machine name] - list of taxonomy terms by vocabulary machine name

For advanced filtering you can use multiple arguments. For example:
```
/rest/os2web/list/node/1+2+3
```
for `AND` condition
```
/rest/os2web/list/node/1,2,3
```
for `OR` condition

**NOTE**: It's not possible to use `AND` condition on filtering taxonomy terms.

## Install
Module is available to download via composer.
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"description": "OS2Web Rest API",
"type": "drupal-module",
"require": {
"drupal/core": "*",
"drupal/restui": "*"
"drupal/core": "*"
},
"license": "EUPL-1.2",
"minimum-stability": "dev"
Expand Down
7 changes: 6 additions & 1 deletion os2web_rest_api.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ description: 'Provides REST API for OS2Web content'
core: 8.x
package: 'OS2Web'
dependencies:
- restui
- rest
- node
- serialization
- taxonomy
- views
- user
6 changes: 6 additions & 0 deletions os2web_rest_api.links.menu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
os2web_rest_api.os2web_reset_api_settings_form:
title: 'OS2Web Rest API'
route_name: os2web_rest_api.os2web_reset_api_settings_form
description: 'Settings form and description on OS2Web Rest API'
parent: system.admin_config_system
weight: 99
1 change: 1 addition & 0 deletions os2web_rest_api.module
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function os2web_rest_api_help($route_name, RouteMatchInterface $route_match) {
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Provides REST API for OS2Web content') . '</p>';
$output .= '<p>' . t('See module information and settings on <a href="/admin/config/system/os2web-rest-api">configuration page</a>') . '</p>';
return $output;

default:
Expand Down
9 changes: 9 additions & 0 deletions os2web_rest_api.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
os2web_rest_api.os2web_reset_api_settings_form:
path: '/admin/config/system/os2web-rest-api'
defaults:
_form: '\Drupal\os2web_rest_api\Form\SettingsForm'
_title_callback: '\Drupal\os2web_rest_api\Form\SettingsForm::getTitle'
requirements:
_permission: 'access administration pages'
options:
_admin_route: TRUE
167 changes: 167 additions & 0 deletions src/Form/SettingsForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php

namespace Drupal\os2web_rest_api\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Core\Render\Markup;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Class SettingsForm.
*/
class SettingsForm extends ConfigFormBase {

/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;

/**
* Constructs a \Drupal\system\ConfigFormBase object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
parent::__construct($config_factory);
$this->moduleHandler = $module_handler;
}

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

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

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

/**
* Title callback.
*/
public static function getTitle() {
return t('OS2Web Rest API Settings');
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['documentation_header'] = [
'#prefix' => '<h3>',
'#markup' => $this->t('Documentation'),
'#suffix' => '</h3>',
];

$form['documentation']= [
'#prefix' => '<p>',
'#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>.'),
'#suffix' => '</p>',
];


$path_array = [
'OS2WEB Rest list node' => '/admin/structure/views/view/os2web_rest_list_node',
'OS2WEB Rest list term' => '/admin/structure/views/view/os2web_rest_list_term',
];
$predefined_url = [
'#theme' => 'item_list',
'#items' => [],
];

foreach ($path_array as $label => $path) {
$predefined_url['#items'][$label] = Link::fromTextAndUrl($label, Url::fromUri('internal:' . $path, ['absolute' => TRUE]))->toString();
}
$form['urls']= [
'#prefix' => '<p>',
'#markup' => $this->t('Predefined views to export list of entities:@list', [
'@list' => Markup::create(\Drupal::service('renderer')->renderPlain($predefined_url)),
]),
'#suffix' => '</p>',
];

$form['auth_header'] = [
'#prefix' => '<h3>',
'#markup' => $this->t('Authorization'),
'#suffix' => '</h3>',
];

if ($this->moduleHandler->moduleExists('basic_auth')) {
$form['basic_auth'] = [
'#prefix' => '<p>',
'#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.', [
'@node_list_api_url' => $predefined_url['#items']['OS2WEB Rest list node'],
'@term_list_api_url' => $predefined_url['#items']['OS2WEB Rest list term'],
]),
'#suffix' => '</p>',
];
}
else {
$form['basic_auth'] = [
'#prefix' => '<p>',
'#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.'),
'#suffix' => '</p>',
];
}


$form['extensions_header'] = [
'#prefix' => '<h3>',
'#markup' => $this->t('Useful extensions'),
'#suffix' => '</h3>',
];

if ($this->moduleHandler->moduleExists('restui')) {
$form['restui'] = [
'#prefix' => '<p>',
'#markup' => $this->t('Get <a href="/admin/config/services/rest">overview on RESTfull API configuration</a>'),
'#suffix' => '</p>',
];
}
else {
$form['restui'] = [
'#prefix' => '<p>',
'#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.'),
'#suffix' => '</p>',
];
}
// There is nothing to save.
// Skipping parent::buildForm($form, $form_state) call.
return $form;
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// There is nothing to save here at the current moment.
parent::submitForm($form, $form_state);
}

}

0 comments on commit f4e2dab

Please sign in to comment.