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_kml' => t('KML export features'),
'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.
farm_client.client_module.*:
# Schema for the configuration files of the farm_fieldkit module.
farm_fieldkit.field_module.*:
type: config_entity
label: 'Client module'
label: 'Field module'
mapping:
id:
type: string

View File

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

View File

@ -2,7 +2,7 @@
/**
* @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;
@ -10,13 +10,14 @@ use Drupal\consumers\Entity\Consumer;
/**
* Implements hook_install().
*/
function farm_client_install() {
function farm_fieldkit_install() {
// Create a consumer for the farmOS Field Kit client.
$fk_consumer = Consumer::create([
'label' => 'farmOS Client (Field Kit)',
'client_id' => 'farm_client',
'label' => 'Field Kit',
'client_id' => 'fieldkit',
'redirect' => 'https://farmOS.app',
'allowed_origins' => 'https://farmos.app',
'owner_id' => '',
'secret' => '',
'confidential' => FALSE,
@ -31,11 +32,11 @@ function farm_client_install() {
/**
* 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')
->loadByProperties(['client_id' => 'farm_client']);
->loadByProperties(['client_id' => 'fieldkit']);
// If found, delete the consumer.
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/test_module.js:
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
namespace Drupal\farm_client\Controller;
namespace Drupal\farm_fieldkit\Controller;
use Drupal\Core\Asset\LibraryDiscoveryInterface;
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\HttpFoundation\Response;
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.
@ -22,7 +22,7 @@ class ClientModuleController extends ControllerBase {
protected $libraryDiscovery;
/**
* ClientModuleController constructor.
* FieldModuleController constructor.
*
* @param \Drupal\Core\Asset\LibraryDiscoveryInterface $library_discovery
* 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
* The ClientModule config entity.
* @param \Drupal\farm_fieldkit\Entity\FieldModuleInterface $field_module
* The FieldtModule config entity.
*
* @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.
$library = $client_module->getLibrary();
// Get the field module library.
$library = $field_module->getLibrary();
[$extension, $name] = explode('/', $library, 2);
$definition = $this->libraryDiscovery->getLibraryByName($extension, $name);
// Bail if no JS library is provided.
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.
$raw = file_get_contents($definition['js'][0]['data']);
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.

View File

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