Merge branch '2.x-fieldkit' into 2.x

This commit is contained in:
Michael Stenta 2021-12-20 17:34:22 -05:00
commit 059e0af8d9
19 changed files with 106 additions and 105 deletions

View File

@ -56,7 +56,7 @@ function farm_modules() {
'farm_medical' => t('Medical logs'), 'farm_medical' => t('Medical logs'),
'farm_kml' => t('KML export features'), 'farm_kml' => t('KML export features'),
'farm_import_kml' => t('KML asset importer'), 'farm_import_kml' => t('KML asset importer'),
'farm_client' => t('Field Kit integration'), 'farm_fieldkit' => t('Field Kit integration'),
], ],
]; ];
} }

View File

@ -1,5 +0,0 @@
farm_client:
config_permissions:
- administer client_module
default_permissions:
- view client_module

View File

@ -1,12 +0,0 @@
farm_client.client_module_js:
path: 'farm/client/js/{client_module}/index.js'
defaults:
_controller: '\Drupal\farm_client\Controller\ClientModuleController::index'
requirements:
# This endpoint is public.
_access: 'TRUE'
methods: [GET]
options:
parameters:
client_module:
type: entity:client_module

View File

@ -1,10 +0,0 @@
langcode: en
status: true
dependencies:
enforced:
module:
- farm_client_test
id: test
label: Test module
description: Just a client module for testing.
library: farm_client_test/test_client_module

View File

@ -1,7 +0,0 @@
name: farmOS Client Test
description: Test client module.
type: module
package: Testing
core_version_requirement: ^9
dependencies:
- farm:farm_client

View File

@ -1,2 +0,0 @@
// This is just a test.
console.log('If you can see this, then the client module was successfully loaded.');

View File

@ -1,31 +0,0 @@
<?php
namespace Drupal\farm_client\Entity;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityDescriptionInterface;
/**
* Provides an interface for defining ClientModule config entities.
*
* @ingroup farm
*/
interface ClientModuleInterface extends ConfigEntityInterface, EntityDescriptionInterface {
/**
* Returns the client module label.
*
* @return string
* The client module label.
*/
public function getLabel();
/**
* Returns the client module library.
*
* @return string
* The client module library.
*/
public function getLibrary();
}

View File

@ -1,7 +1,7 @@
# Schema for the configuration files of the farm_client module. # Schema for the configuration files of the farm_fieldkit module.
farm_client.client_module.*: farm_fieldkit.field_module.*:
type: config_entity type: config_entity
label: 'Client module' label: 'Field module'
mapping: mapping:
id: id:
type: string type: string

View File

@ -1,5 +1,5 @@
name: farmOS Client name: farmOS Field Kit
description: Integrates the farmOS server and client. description: Allows Field Kit to connect to this farmOS server.
type: module type: module
package: farmOS package: farmOS
core_version_requirement: ^9 core_version_requirement: ^9

View File

@ -2,7 +2,7 @@
/** /**
* @file * @file
* Install, update and uninstall function for the farm_client module. * Install, update and uninstall function for the farm_fieldkit module.
*/ */
use Drupal\consumers\Entity\Consumer; use Drupal\consumers\Entity\Consumer;
@ -10,13 +10,14 @@ use Drupal\consumers\Entity\Consumer;
/** /**
* Implements hook_install(). * Implements hook_install().
*/ */
function farm_client_install() { function farm_fieldkit_install() {
// Create a consumer for the farmOS Field Kit client. // Create a consumer for the farmOS Field Kit client.
$fk_consumer = Consumer::create([ $fk_consumer = Consumer::create([
'label' => 'farmOS Client (Field Kit)', 'label' => 'Field Kit',
'client_id' => 'farm_client', 'client_id' => 'fieldkit',
'redirect' => 'https://farmOS.app', 'redirect' => 'https://farmOS.app',
'allowed_origins' => 'https://farmos.app',
'owner_id' => '', 'owner_id' => '',
'secret' => '', 'secret' => '',
'confidential' => FALSE, 'confidential' => FALSE,
@ -31,11 +32,11 @@ function farm_client_install() {
/** /**
* Implements hook_uninstall(). * Implements hook_uninstall().
*/ */
function farm_client_uninstall() { function farm_fieldkit_uninstall() {
// Load the default farm consumer. // Load the fieldkit consumer.
$consumers = \Drupal::entityTypeManager()->getStorage('consumer') $consumers = \Drupal::entityTypeManager()->getStorage('consumer')
->loadByProperties(['client_id' => 'farm_client']); ->loadByProperties(['client_id' => 'fieldkit']);
// If found, delete the consumer. // If found, delete the consumer.
if (!empty($consumers)) { if (!empty($consumers)) {

View File

@ -0,0 +1,5 @@
farm_fieldkit:
config_permissions:
- administer field_module
default_permissions:
- view field_module

View File

@ -0,0 +1,12 @@
farm_fieldkit.field_module_js:
path: 'fieldkit/js/{field_module}/index.js'
defaults:
_controller: '\Drupal\farm_fieldkit\Controller\FieldModuleController::index'
requirements:
# This endpoint is public.
_access: 'TRUE'
methods: [GET]
options:
parameters:
field_module:
type: entity:field_module

View File

@ -0,0 +1,10 @@
langcode: en
status: true
dependencies:
enforced:
module:
- farm_fieldkit_test
id: test
label: Test module
description: Just a field module for testing.
library: farm_fieldkit_test/test_field_module

View File

@ -0,0 +1,7 @@
name: farmOS Field Kit Test
description: Test field module.
type: module
package: Testing
core_version_requirement: ^9
dependencies:
- farm:farm_fieldkit

View File

@ -1,4 +1,4 @@
test_client_module: test_field_module:
js: js:
js/test_module.js: js/test_module.js:
preprocess: false preprocess: false

View File

@ -0,0 +1,2 @@
// This is just a test.
console.log('If you can see this, then the field module was successfully loaded.');

View File

@ -1,18 +1,18 @@
<?php <?php
namespace Drupal\farm_client\Controller; namespace Drupal\farm_fieldkit\Controller;
use Drupal\Core\Asset\LibraryDiscoveryInterface; use Drupal\Core\Asset\LibraryDiscoveryInterface;
use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Controller\ControllerBase;
use Drupal\farm_client\Entity\ClientModuleInterface; use Drupal\farm_fieldkit\Entity\FieldModuleInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
/** /**
* Serves JavaScript files for client modules. * Serves JavaScript files for field modules.
*/ */
class ClientModuleController extends ControllerBase { class FieldModuleController extends ControllerBase {
/** /**
* The library discovery service. * The library discovery service.
@ -22,7 +22,7 @@ class ClientModuleController extends ControllerBase {
protected $libraryDiscovery; protected $libraryDiscovery;
/** /**
* ClientModuleController constructor. * FieldModuleController constructor.
* *
* @param \Drupal\Core\Asset\LibraryDiscoveryInterface $library_discovery * @param \Drupal\Core\Asset\LibraryDiscoveryInterface $library_discovery
* The library discovery service. * The library discovery service.
@ -41,30 +41,30 @@ class ClientModuleController extends ControllerBase {
} }
/** /**
* Returns the ClientModule JS. * Returns the FieldModule JS.
* *
* @param \Drupal\farm_client\Entity\ClientModuleInterface $client_module * @param \Drupal\farm_fieldkit\Entity\FieldModuleInterface $field_module
* The ClientModule config entity. * The FieldtModule config entity.
* *
* @return \Symfony\Component\HttpFoundation\Response * @return \Symfony\Component\HttpFoundation\Response
* A response containing the ClientModule JS or a 422 error response. * A response containing the FieldModule JS or a 422 error response.
*/ */
public function index(ClientModuleInterface $client_module) { public function index(FieldModuleInterface $field_module) {
// Get the client module library. // Get the field module library.
$library = $client_module->getLibrary(); $library = $field_module->getLibrary();
[$extension, $name] = explode('/', $library, 2); [$extension, $name] = explode('/', $library, 2);
$definition = $this->libraryDiscovery->getLibraryByName($extension, $name); $definition = $this->libraryDiscovery->getLibraryByName($extension, $name);
// Bail if no JS library is provided. // Bail if no JS library is provided.
if (empty($definition['js'])) { if (empty($definition['js'])) {
throw new UnprocessableEntityHttpException('The client module does not have a valid JS library configured.'); throw new UnprocessableEntityHttpException('The field module does not have a valid JS library configured.');
} }
// Try loading the raw JS data. // Try loading the raw JS data.
$raw = file_get_contents($definition['js'][0]['data']); $raw = file_get_contents($definition['js'][0]['data']);
if (empty($raw)) { if (empty($raw)) {
throw new UnprocessableEntityHttpException('The client module JS library could not be loaded.'); throw new UnprocessableEntityHttpException('The field module JS library could not be loaded.');
} }
// Return a response with the JS asset. // Return a response with the JS asset.

View File

@ -1,16 +1,16 @@
<?php <?php
namespace Drupal\farm_client\Entity; namespace Drupal\farm_fieldkit\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\Core\Config\Entity\ConfigEntityBase;
/** /**
* Defines the ClientModule entity. * Defines the FieldModule entity.
* *
* @ConfigEntityType( * @ConfigEntityType(
* id = "client_module", * id = "field_module",
* label = @Translation("Client module"), * label = @Translation("Field module"),
* label_collection = @Translation("Client modules"), * label_collection = @Translation("Field modules"),
* handlers = { * handlers = {
* "access" = "\Drupal\entity\EntityAccessControlHandler", * "access" = "\Drupal\entity\EntityAccessControlHandler",
* "permission_provider" = "\Drupal\entity\EntityPermissionProvider", * "permission_provider" = "\Drupal\entity\EntityPermissionProvider",
@ -29,31 +29,31 @@ use Drupal\Core\Config\Entity\ConfigEntityBase;
* *
* @ingroup farm * @ingroup farm
*/ */
class ClientModule extends ConfigEntityBase implements ClientModuleInterface { class FieldModule extends ConfigEntityBase implements FieldModuleInterface {
/** /**
* The client module ID. * The field module ID.
* *
* @var string * @var string
*/ */
protected $id; protected $id;
/** /**
* The client module label. * The field module label.
* *
* @var string * @var string
*/ */
protected $label; protected $label;
/** /**
* A brief description of this client module. * A brief description of this field module.
* *
* @var string * @var string
*/ */
protected $description; protected $description;
/** /**
* The client module library name. * The field module library name.
* *
* @var string * @var string
*/ */

View File

@ -0,0 +1,31 @@
<?php
namespace Drupal\farm_fieldkit\Entity;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityDescriptionInterface;
/**
* Provides an interface for defining FieldModule config entities.
*
* @ingroup farm
*/
interface FieldModuleInterface extends ConfigEntityInterface, EntityDescriptionInterface {
/**
* Returns the field module label.
*
* @return string
* The field module label.
*/
public function getLabel();
/**
* Returns the field module library.
*
* @return string
* The field module library.
*/
public function getLibrary();
}