3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00

Use the contrib entity bundle class instead of custom data stream plugin.

This commit is contained in:
paul121 2021-03-10 15:49:48 -08:00 committed by Michael Stenta
parent 3fca66b83b
commit 783d547cc0
14 changed files with 170 additions and 148 deletions

View file

@ -1,18 +0,0 @@
langcode: en
status: true
dependencies:
config:
- field.storage.data_stream.public_key
- data_stream.type.legacy_listener
id: data_stream.legacy_listener.public_key
field_name: public_key
entity_type: data_stream
bundle: legacy_listener
label: Public key
description: 'Public key used to identify this data stream in the legacy listener endpoint.'
required: false
translatable: false
default_value: { }
default_value_callback: ''
settings: { }
field_type: string

View file

@ -1,20 +0,0 @@
langcode: en
status: true
dependencies:
module:
- data_stream
id: data_stream.public_key
field_name: public_key
entity_type: data_stream
type: string
settings:
max_length: 255
is_ascii: false
case_sensitive: false
module: core
locked: false
cardinality: 1
translatable: false
indexes: { }
persist_with_no_fields: false
custom_storage: false

View file

@ -1,14 +1,15 @@
<?php
namespace Drupal\farm_sensor_listener\Plugin\DataStream;
namespace Drupal\farm_sensor_listener\Plugin\DataStream\DataStreamType;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Database\Connection;
use Drupal\Core\Form\FormStateInterface;
use Drupal\data_stream\DataStreamStorageInterface;
use Drupal\data_stream\DataStreamPluginBase;
use Drupal\data_stream\Entity\DataStreamInterface;
use Drupal\data_stream\Plugin\DataStream\DataStreamType\DataStreamTypeBase;
use Drupal\data_stream\Traits\DataStreamPrivateKeyAccess;
use Drupal\entity\BundleFieldDefinition;
use Drupal\farm_sensor_listener\LegacySensorApiInterface;
use Drupal\fraction\Fraction;
use Drupal\jsonapi\Exception\UnprocessableHttpEntityException;
@ -21,14 +22,14 @@ use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
/**
* DataStream plugin that provides legacy listener behavior.
* Provides the legacy listener data stream type.
*
* @DataStream(
* @DataStreamType(
* id = "legacy_listener",
* label = @Translation("Legacy listener"),
* )
*/
class LegacyListenerDataStream extends DataStreamPluginBase implements DataStreamStorageInterface, LegacySensorApiInterface {
class LegacyListener extends DataStreamTypeBase implements DataStreamStorageInterface, LegacySensorApiInterface {
use DataStreamPrivateKeyAccess;
@ -75,6 +76,37 @@ class LegacyListenerDataStream extends DataStreamPluginBase implements DataStrea
);
}
/**
* {@inheritdoc}
*/
public function buildFieldDefinitions() {
$fields = [];
// Define the public_key field.
$field = BundleFieldDefinition::create('string')
->setLabel($this->t('Public key'))
->setDescription($this->t('Public key used to identify this data stream in the legacy listener endpoint.'))
->setSetting('max_length', 255)
->setSetting('is_ascii', FALSE)
->setSetting('case_sensitive', FALSE)
->setDisplayOptions('form', [
'type' => 'string_textfield',
'settings' => [
'size' => 60,
'placeholder' => '',
],
'weight' => $options['weight']['form'] ?? 0,
])
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => $options['weight']['view'] ?? 0,
]);
$fields['public_key'] = $field;
return $fields;
}
/**
* {@inheritdoc}
*/

View file

@ -1,4 +1,4 @@
services:
plugin.manager.data_stream:
class: Drupal\data_stream\DataStreamPluginManager
plugin.manager.data_stream_type:
class: Drupal\data_stream\DataStreamTypeManager
parent: default_plugin_manager

View file

@ -1,28 +0,0 @@
<?php
namespace Drupal\data_stream\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines a DataStream annotation object.
*
* @Annotation
*/
class DataStream extends Plugin {
/**
* The DataStream plugin id.
*
* @var string
*/
public $id;
/**
* The DataStream plugin label.
*
* @var string
*/
public $label;
}

View file

@ -0,0 +1,34 @@
<?php
namespace Drupal\data_stream\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines the data stream type plugin annotation object.
*
* Plugin namespace: Plugin\DataStream\DataStreamType.
*
* @see plugin_api
*
* @Annotation
*/
class DataStreamType extends Plugin {
/**
* The plugin ID.
*
* @var string
*/
public $id;
/**
* The data stream type label.
*
* @var \Drupal\Core\Annotation\Translation
*
* @ingroup plugin_translatable
*/
public $label;
}

View file

@ -1,13 +0,0 @@
<?php
namespace Drupal\data_stream;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginFormInterface;
/**
* An interface for DataStream Plugins.
*/
interface DataStreamPluginInterface extends ContainerFactoryPluginInterface, PluginFormInterface {
}

View file

@ -1,36 +0,0 @@
<?php
namespace Drupal\data_stream;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
/**
* The DataStreamPluginManager service.
*/
class DataStreamPluginManager extends DefaultPluginManager {
/**
* Constructs a SensorTypeManager object.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct(
'Plugin/DataStream',
$namespaces,
$module_handler,
'Drupal\data_stream\DataStreamPluginInterface',
'Drupal\data_stream\Annotation\DataStream'
);
$this->setCacheBackend($cache_backend, 'data_stream_plugins');
}
}

View file

@ -0,0 +1,49 @@
<?php
namespace Drupal\data_stream;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
/**
* Manages discovery and instantiation of data stream type plugins.
*
* @see \Drupal\data_stream\Annotation\DataStreamType
* @see plugin_api
*/
class DataStreamTypeManager extends DefaultPluginManager {
/**
* Constructs a new DataStreamTypeManager object.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* The cache backend.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/DataStream/DataStreamType', $namespaces, $module_handler, 'Drupal\data_stream\Plugin\DataStream\DataStreamType\DataStreamTypeInterface', 'Drupal\data_stream\Annotation\DataStreamType');
$this->alterInfo('data_stream_type_info');
$this->setCacheBackend($cache_backend, 'data_stream_type_plugins');
}
/**
* {@inheritdoc}
*/
public function processDefinition(&$definition, $plugin_id) {
parent::processDefinition($definition, $plugin_id);
foreach (['id', 'label'] as $required_property) {
if (empty($definition[$required_property])) {
throw new PluginException(sprintf('The data stream type %s must define the %s property.', $plugin_id, $required_property));
}
}
}
}

View file

@ -53,6 +53,7 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface;
* "langcode" = "langcode",
* },
* bundle_entity_type = "data_stream_type",
* bundle_plugin_type = "data_stream_type",
* common_reference_target = TRUE,
* permission_granularity = "bundle",
* links = {
@ -81,9 +82,8 @@ class DataStream extends ContentEntityBase implements DataStreamInterface {
* {@inheritdoc}
*/
public function getPlugin() {
/** @var \Drupal\data_stream\Entity\DataStreamTypeInterface $plugin */
$plugin = \Drupal::service('plugin.manager.data_stream')->createInstance($this->bundle());
return $plugin;
/** @var \Drupal\data_stream\Plugin\DataStream\DataStreamType\DataStreamTypeInterface $plugin */
return \Drupal::service('plugin.manager.data_stream_type')->createInstance($this->bundle());
}
/**

View file

@ -11,10 +11,10 @@ use Drupal\Core\Entity\EntityChangedInterface;
interface DataStreamInterface extends ContentEntityInterface, EntityChangedInterface {
/**
* Returns an instance of the data stream plugin.
* Returns an instance of the data stream bundle plugin.
*
* @return \Drupal\data_stream\DataStreamPluginInterface
* The data stream plugin.
* @return \Drupal\data_stream\Plugin\DataStream\DataStreamType\DataStreamTypeInterface
* The data stream bundle plugin.
*/
public function getPlugin();

View file

@ -1,25 +1,28 @@
<?php
namespace Drupal\data_stream;
namespace Drupal\data_stream\Plugin\DataStream\DataStreamType;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Base class for DataStream plugins.
* Provides the base data stream type class.
*/
class DataStreamPluginBase extends PluginBase implements DataStreamPluginInterface {
abstract class DataStreamTypeBase extends PluginBase implements ContainerFactoryPluginInterface, DataStreamTypeInterface {
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
);
public function getLabel() {
return $this->pluginDefinition['label'];
}
/**
* {@inheritdoc}
*/
public function buildFieldDefinitions() {
return [];
}
/**
@ -28,15 +31,14 @@ class DataStreamPluginBase extends PluginBase implements DataStreamPluginInterfa
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
// Get the plugin label.
$definition = $this->getPluginDefinition();
$label = $definition['label'];
$label = $this->getLabel();
// Render a fieldset for the plugin specific settings.
$form[$this->getPluginId()] = [
'#type' => 'fieldset',
'#title' => $label,
'#type' => 'fieldset',
'#title' => $label,
'#description' => $this->t('Settings for the %type data stream type.', ['%type' => $label]),
'#weight' => 10,
'#weight' => 10,
];
return $form;
}

View file

@ -0,0 +1,21 @@
<?php
namespace Drupal\data_stream\Plugin\DataStream\DataStreamType;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\entity\BundlePlugin\BundlePluginInterface;
/**
* Defines the interface for data stream types.
*/
interface DataStreamTypeInterface extends BundlePluginInterface, PluginFormInterface {
/**
* Gets the data stream type label.
*
* @return string
* The data stream type label.
*/
public function getLabel();
}

View file

@ -1,12 +1,11 @@
<?php
namespace Drupal\data_stream\Plugin\DataStream;
namespace Drupal\data_stream\Plugin\DataStream\DataStreamType;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Database\Connection;
use Drupal\data_stream\DataStreamApiInterface;
use Drupal\data_stream\DataStreamStorageInterface;
use Drupal\data_stream\DataStreamPluginBase;
use Drupal\data_stream\Entity\DataStreamInterface;
use Drupal\data_stream\Traits\DataStreamSqlStorage;
use Drupal\data_stream\Traits\DataStreamPrivateKeyAccess;
@ -20,14 +19,14 @@ use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
/**
* DataStream plugin that provides listener behavior.
* Provides the listener data stream type.
*
* @DataStream(
* @DataStreamType(
* id = "listener",
* label = @Translation("Listener"),
* )
*/
class ListenerDataStream extends DataStreamPluginBase implements DataStreamStorageInterface, DataStreamApiInterface {
class Listener extends DataStreamTypeBase implements DataStreamStorageInterface, DataStreamApiInterface {
use DataStreamSqlStorage;
use DataStreamPrivateKeyAccess;