clean install

This commit is contained in:
2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 changed files with 3555714 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
# Schema for the configuration files of the farm_fieldkit module.
farm_fieldkit.field_module.*:
type: config_entity
label: 'Field module'
mapping:
id:
type: string
label: 'Machine-readable name'
label:
type: label
label: 'Type'
description:
type: text
label: 'Description'
library:
type: string
label: 'Library name'

View File

@@ -0,0 +1,9 @@
name: farmOS Field Kit
description: Allows Field Kit to connect to this farmOS server.
type: module
package: farmOS
core_version_requirement: ^10
dependencies:
- entity:entity
- farm:farm_api
- simple_oauth_password_grant:simple_oauth_password_grant

View File

@@ -0,0 +1,57 @@
<?php
/**
* @file
* Install, update and uninstall function for the farm_fieldkit module.
*/
use Drupal\consumers\Entity\Consumer;
use Drupal\simple_oauth\Oauth2ScopeInterface;
/**
* Implements hook_install().
*/
function farm_fieldkit_install() {
// Check for default role scopes.
/** @var \Drupal\simple_oauth\Oauth2ScopeProviderInterface $scope_provider */
$scope_provider = \Drupal::service('simple_oauth.oauth2_scope.provider');
$scopes = $scope_provider->loadMultiple(['farm_manager', 'farm_worker']);
$scope_ids = array_map(function (Oauth2ScopeInterface $scope) {
return $scope->id();
}, $scopes);
// Create a consumer for the farmOS Field Kit client.
$fk_consumer = Consumer::create([
'label' => 'Field Kit',
'client_id' => 'fieldkit',
'access_token_expiration' => 3600,
'grant_types' => [
'refresh_token',
'password',
],
'scopes' => array_values($scope_ids),
'redirect' => 'https://farmOS.app',
'allowed_origins' => 'https://farmos.app',
'secret' => NULL,
'confidential' => FALSE,
'third_party' => FALSE,
]);
$fk_consumer->save();
}
/**
* Implements hook_uninstall().
*/
function farm_fieldkit_uninstall() {
// Load the fieldkit consumer.
$consumers = \Drupal::entityTypeManager()->getStorage('consumer')
->loadByProperties(['client_id' => 'fieldkit']);
// If found, delete the consumer.
if (!empty($consumers)) {
$client_consumer = reset($consumers);
$client_consumer->delete();
}
}

View File

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

View File

@@ -0,0 +1,38 @@
<?php
/**
* @file
* Post update functions for farm_fieldkit module.
*/
use Drupal\simple_oauth\Oauth2ScopeInterface;
/**
* Enable simple oauth password grant.
*/
function farm_fieldkit_post_update_enable_password_grant(&$sandbox = NULL) {
// Enable password grant module.
if (!\Drupal::service('module_handler')->moduleExists('simple_oauth_password_grant')) {
\Drupal::service('module_installer')->install(['simple_oauth_password_grant']);
}
// Check for default role scopes.
/** @var \Drupal\simple_oauth\Oauth2ScopeProviderInterface $scope_provider */
$scope_provider = \Drupal::service('simple_oauth.oauth2_scope.provider');
$scopes = $scope_provider->loadMultiple(['farm_manager', 'farm_worker']);
$scope_ids = array_map(function (Oauth2ScopeInterface $scope) {
return $scope->id();
}, $scopes);
// Update existing fieldkit consumer.
$consumers = \Drupal::entityTypeManager()->getStorage('consumer')
->loadByProperties(['client_id' => 'fieldkit']);
if (!empty($consumers)) {
/** @var \Drupal\consumers\Entity\ConsumerInterface $fieldkit */
$fieldkit = reset($consumers);
$fieldkit->set('grant_types', ['refresh_token', 'password']);
$fieldkit->set('scopes', array_values($scope_ids));
$fieldkit->save();
}
}

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: ^10
dependencies:
- farm:farm_fieldkit

View File

@@ -0,0 +1,4 @@
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

@@ -0,0 +1,77 @@
<?php
namespace Drupal\farm_fieldkit\Controller;
use Drupal\Core\Asset\LibraryDiscoveryInterface;
use Drupal\Core\Controller\ControllerBase;
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 field modules.
*/
class FieldModuleController extends ControllerBase {
/**
* The library discovery service.
*
* @var \Drupal\Core\Asset\LibraryDiscoveryInterface
*/
protected $libraryDiscovery;
/**
* FieldModuleController constructor.
*
* @param \Drupal\Core\Asset\LibraryDiscoveryInterface $library_discovery
* The library discovery service.
*/
public function __construct(LibraryDiscoveryInterface $library_discovery) {
$this->libraryDiscovery = $library_discovery;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('library.discovery')
);
}
/**
* Returns the FieldModule JS.
*
* @param \Drupal\farm_fieldkit\Entity\FieldModuleInterface $field_module
* The FieldtModule config entity.
*
* @return \Symfony\Component\HttpFoundation\Response
* A response containing the FieldModule JS or a 422 error response.
*/
public function index(FieldModuleInterface $field_module) {
// 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 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 field module JS library could not be loaded.');
}
// Return a response with the JS asset.
$response = new Response();
$response->headers->set('Content-Type', 'application/javascript; utf-8');
$response->setContent($raw);
return $response;
}
}

View File

@@ -0,0 +1,90 @@
<?php
namespace Drupal\farm_fieldkit\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
/**
* Defines the FieldModule entity.
*
* @ConfigEntityType(
* id = "field_module",
* label = @Translation("Field module"),
* label_collection = @Translation("Field modules"),
* handlers = {
* "access" = "\Drupal\entity\EntityAccessControlHandler",
* "permission_provider" = "\Drupal\entity\EntityPermissionProvider",
* },
* entity_keys = {
* "id" = "id",
* "label" = "label",
* },
* config_export = {
* "id",
* "label",
* "description",
* "library",
* },
* )
*
* @ingroup farm
*/
class FieldModule extends ConfigEntityBase implements FieldModuleInterface {
/**
* The field module ID.
*
* @var string
*/
protected $id;
/**
* The field module label.
*
* @var string
*/
protected $label;
/**
* A brief description of this field module.
*
* @var string
*/
protected $description;
/**
* The field module library name.
*
* @var string
*/
protected $library;
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->description;
}
/**
* {@inheritdoc}
*/
public function setDescription($description) {
return $this->set('description', $description);
}
/**
* {@inheritdoc}
*/
public function getLabel() {
return $this->label;
}
/**
* {@inheritdoc}
*/
public function getLibrary() {
return $this->library;
}
}

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();
}