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

Fix \Drupal calls should be avoided in classes, use dependency injection instead.

This commit is contained in:
Michael Stenta 2022-09-12 12:12:43 -04:00
parent ef2bc30feb
commit ff60b3164d
16 changed files with 456 additions and 39 deletions

View file

@ -2,6 +2,7 @@
namespace Drupal\farm_api\Controller;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ProfileExtensionList;
use Drupal\Core\Session\AccountInterface;
use Drupal\jsonapi\CacheableResourceResponse;
@ -30,6 +31,13 @@ class FarmEntryPoint extends EntryPoint {
*/
protected $farmProfileInfo;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* EntryPoint constructor.
*
@ -39,10 +47,13 @@ class FarmEntryPoint extends EntryPoint {
* The current user.
* @param \Drupal\Core\Extension\ProfileExtensionList $profile_extension_list
* The profile extension list service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
public function __construct(ResourceTypeRepositoryInterface $resource_type_repository, AccountInterface $user, ProfileExtensionList $profile_extension_list) {
public function __construct(ResourceTypeRepositoryInterface $resource_type_repository, AccountInterface $user, ProfileExtensionList $profile_extension_list, ModuleHandlerInterface $module_handler) {
parent::__construct($resource_type_repository, $user);
$this->farmProfileInfo = $profile_extension_list->getExtensionInfo('farm');
$this->moduleHandler = $module_handler;
}
/**
@ -52,7 +63,8 @@ class FarmEntryPoint extends EntryPoint {
return new static(
$container->get('jsonapi.resource_type.repository'),
$container->get('current_user'),
$container->get('extension.list.profile')
$container->get('extension.list.profile'),
$container->get('module_handler'),
);
}
@ -82,7 +94,7 @@ class FarmEntryPoint extends EntryPoint {
];
// Allow modules to add additional meta information.
\Drupal::moduleHandler()->alter('farm_api_meta', $meta['farm']);
$this->moduleHandler->alter('farm_api_meta', $meta['farm']);
// Build a new response.
$new_response = new CacheableResourceResponse(new JsonApiDocumentTopLevel(new ResourceObjectData([]), new NullIncludedData(), $urls, $meta));

View file

@ -2,8 +2,18 @@
namespace Drupal\asset;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines the controller class for assets.
@ -13,6 +23,57 @@ use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
*/
class AssetStorage extends SqlContentEntityStorage {
/**
* The time service.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
/**
* Constructs an AssetStorage object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Database\Connection $database
* The database connection to be used.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* The cache backend to be used.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface $memory_cache
* The memory cache backend to be used.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle info.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
*/
public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityFieldManagerInterface $entity_field_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, MemoryCacheInterface $memory_cache, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityTypeManagerInterface $entity_type_manager, TimeInterface $time) {
parent::__construct($entity_type, $database, $entity_field_manager, $cache, $language_manager, $memory_cache, $entity_type_bundle_info, $entity_type_manager);
$this->time = $time;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entity_type,
$container->get('database'),
$container->get('entity_field.manager'),
$container->get('cache.entity'),
$container->get('language_manager'),
$container->get('entity.memory_cache'),
$container->get('entity_type.bundle.info'),
$container->get('entity_type.manager'),
$container->get('datetime.time'),
);
}
/**
* {@inheritdoc}
*/
@ -53,7 +114,7 @@ class AssetStorage extends SqlContentEntityStorage {
// If the state has changed to archived and no archived timestamp was
// specified, set it to the current time.
if ($new_state == 'archived' && $entity->getArchivedTime() == NULL) {
$entity->setArchivedTime(\Drupal::time()->getRequestTime());
$entity->setArchivedTime($this->time->getRequestTime());
}
// Or, if the state has changed from archived, set a null value.

View file

@ -2,6 +2,7 @@
namespace Drupal\data_stream\Plugin\DataStream\DataStreamType;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Database\Connection;
use Drupal\data_stream\DataStreamApiInterface;
@ -49,6 +50,13 @@ class Basic extends DataStreamTypeBase implements DataStreamStorageInterface, Da
*/
protected $eventDispatcher;
/**
* The time service.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
/**
* Database table.
*
@ -71,11 +79,14 @@ class Basic extends DataStreamTypeBase implements DataStreamStorageInterface, Da
* The database connection.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher service.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
*/
public function __construct(array $configuration, string $plugin_id, $plugin_definition, Connection $connection, EventDispatcherInterface $event_dispatcher) {
public function __construct(array $configuration, string $plugin_id, $plugin_definition, Connection $connection, EventDispatcherInterface $event_dispatcher, TimeInterface $time) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->connection = $connection;
$this->eventDispatcher = $event_dispatcher;
$this->time = $time;
}
/**
@ -88,6 +99,7 @@ class Basic extends DataStreamTypeBase implements DataStreamStorageInterface, Da
$plugin_definition,
$container->get('database'),
$container->get('event_dispatcher'),
$container->get('datetime.time'),
);
}
@ -442,7 +454,7 @@ class Basic extends DataStreamTypeBase implements DataStreamStorageInterface, Da
// Generate a timestamp from the request time. This will only be used if a
// timestamp is not provided in the JSON data.
if (empty($timestamp)) {
$timestamp = \Drupal::time()->getRequestTime();
$timestamp = $this->time->getRequestTime();
}
// Iterate over the JSON properties.

View file

@ -45,7 +45,7 @@ class DataStream extends EntityContentBase {
$providing_asset = $row->getDestinationProperty('providing_asset');
/** @var \Drupal\asset\Entity\AssetInterface $asset */
$asset = \Drupal::entityTypeManager()->getStorage('asset')->load($providing_asset);
$asset = $this->storage->load($providing_asset);
// Update the assets data_stream field if the asset was found
// and the asset type has the field.

View file

@ -2,9 +2,12 @@
namespace Drupal\data_stream\Plugin\migrate\process;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Gets the first data stream associated with an asset.
@ -16,7 +19,43 @@ use Drupal\migrate\Row;
* id = "data_stream_from_asset"
* )
*/
class DataStreamFromAsset extends ProcessPluginBase {
class DataStreamFromAsset extends ProcessPluginBase implements ContainerFactoryPluginInterface {
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a DataStreamFromAsset object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager'),
);
}
/**
* {@inheritdoc}
@ -32,7 +71,7 @@ class DataStreamFromAsset extends ProcessPluginBase {
}
// Load asset.
$asset = \Drupal::entityTypeManager()->getStorage('asset')->load($asset_id);
$asset = $this->entityTypeManager->getStorage('asset')->load($asset_id);
// Return the first data stream ID if one exists.
if (!empty($asset) && $asset->hasField('data_stream')) {

View file

@ -2,7 +2,11 @@
namespace Drupal\farm_entity\BundlePlugin;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\entity\BundlePlugin\BundlePluginHandler;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Extends BundlePluginHandler to invoke hook_farm_entity_bundle_field_info().
@ -11,6 +15,39 @@ use Drupal\entity\BundlePlugin\BundlePluginHandler;
*/
class FarmEntityBundlePluginHandler extends BundlePluginHandler {
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructs a new FarmEntityBundlePluginHandler object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
* @param \Drupal\Component\Plugin\PluginManagerInterface $plugin_manager
* The bundle plugin manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
public function __construct(EntityTypeInterface $entity_type, PluginManagerInterface $plugin_manager, ModuleHandlerInterface $module_handler) {
parent::__construct($entity_type, $plugin_manager);
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entity_type,
$container->get('plugin.manager.' . $entity_type->get('bundle_plugin_type')),
$container->get('module_handler'),
);
}
/**
* {@inheritdoc}
*/
@ -19,7 +56,7 @@ class FarmEntityBundlePluginHandler extends BundlePluginHandler {
// Allow modules to add definitions.
foreach (array_keys($this->pluginManager->getDefinitions()) as $plugin_id) {
$definitions += \Drupal::moduleHandler()->invokeAll('farm_entity_bundle_field_info', [$this->entityType, $plugin_id]);
$definitions += $this->moduleHandler->invokeAll('farm_entity_bundle_field_info', [$this->entityType, $plugin_id]);
}
// Ensure the presence of required keys which aren't set by the plugin.
@ -42,7 +79,7 @@ class FarmEntityBundlePluginHandler extends BundlePluginHandler {
public function getFieldDefinitions($bundle) {
// Allow modules to add definitions.
$definitions = \Drupal::moduleHandler()->invokeAll('farm_entity_bundle_field_info', [$this->entityType, $bundle]);
$definitions = $this->moduleHandler->invokeAll('farm_entity_bundle_field_info', [$this->entityType, $bundle]);
// Ensure the presence of required keys which aren't set by the plugin.
// This is copied directly from the parent method for consistency.

View file

@ -13,6 +13,7 @@ use Drupal\Core\Url;
use Drupal\log\Entity\Log;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides an asset move confirmation form.
@ -54,6 +55,13 @@ class AssetMoveActionForm extends ConfirmFormBase {
*/
protected $entities;
/**
* The current Request object.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* Constructs an AssetMoveActionForm form object.
*
@ -63,11 +71,14 @@ class AssetMoveActionForm extends ConfirmFormBase {
* The entity type manager.
* @param \Drupal\Core\Session\AccountInterface $user
* The current user.
* @param \Symfony\Component\HttpFoundation\Request $request
* The current Request object.
*/
public function __construct(PrivateTempStoreFactory $temp_store_factory, EntityTypeManagerInterface $entity_type_manager, AccountInterface $user) {
public function __construct(PrivateTempStoreFactory $temp_store_factory, EntityTypeManagerInterface $entity_type_manager, AccountInterface $user, Request $request) {
$this->tempStore = $temp_store_factory->get('asset_move_confirm');
$this->entityTypeManager = $entity_type_manager;
$this->user = $user;
$this->request = $request;
}
/**
@ -77,7 +88,8 @@ class AssetMoveActionForm extends ConfirmFormBase {
return new static(
$container->get('tempstore.private'),
$container->get('entity_type.manager'),
$container->get('current_user')
$container->get('current_user'),
$container->get('request_stack')->getCurrentRequest(),
);
}
@ -130,8 +142,7 @@ class AssetMoveActionForm extends ConfirmFormBase {
public function buildForm(array $form, FormStateInterface $form_state) {
// Check if asset IDs were provided in the asset query param.
$request = \Drupal::request();
if ($asset_ids = $request->get('asset')) {
if ($asset_ids = $this->request->get('asset')) {
// Wrap in an array, if necessary.
if (!is_array($asset_ids)) {

View file

@ -4,6 +4,7 @@ namespace Drupal\farm_map\Plugin\Field\FieldWidget;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\File\FileSystem;
@ -38,6 +39,13 @@ class GeofieldWidget extends GeofieldBaseWidget {
*/
protected $fileSystem;
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Supported GeoPHP file types.
*
@ -74,10 +82,13 @@ class GeofieldWidget extends GeofieldBaseWidget {
* The geofieldBackendManager.
* @param \Drupal\Core\File\FileSystem $file_system
* The file system service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, GeoPHPInterface $geophp_wrapper, WktGeneratorInterface $wkt_generator, GeofieldBackendManager $geofield_backend_manager, FileSystem $file_system) {
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, GeoPHPInterface $geophp_wrapper, WktGeneratorInterface $wkt_generator, GeofieldBackendManager $geofield_backend_manager, FileSystem $file_system, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings, $geophp_wrapper, $wkt_generator, $geofield_backend_manager);
$this->fileSystem = $file_system;
$this->entityTypeManager = $entity_type_manager;
}
/**
@ -93,7 +104,8 @@ class GeofieldWidget extends GeofieldBaseWidget {
$container->get('geofield.geophp'),
$container->get('geofield.wkt_generator'),
$container->get('plugin.manager.geofield_backend'),
$container->get('file_system')
$container->get('file_system'),
$container->get('entity_type.manager'),
);
}
@ -221,7 +233,7 @@ class GeofieldWidget extends GeofieldBaseWidget {
// Load and process each file.
/** @var \Drupal\file\Entity\File[] $files */
$files = \Drupal::entityTypeManager()->getStorage('file')->loadMultiple($file_ids);
$files = $this->entityTypeManager->getStorage('file')->loadMultiple($file_ids);
// @todo Support geometry field with > 1 cardinality.
$wkt_strings = [];

View file

@ -2,9 +2,14 @@
namespace Drupal\farm_migrate\Plugin\migrate\process;
use Drupal\migrate\MigrateLookupInterface;
use Drupal\migrate\MigrateStubInterface;
use Drupal\migrate\Plugin\migrate\process\MigrationLookup;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Looks up the value of a property based on a previous migration group.
@ -32,6 +37,51 @@ use Drupal\migrate\Row;
*/
class FarmMigrationGroupLookup extends MigrationLookup {
/**
* Migration plugin manager service.
*
* @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
*/
protected $migrationPluginManager;
/**
* Constructs a MigrationLookup object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\migrate\Plugin\MigrationInterface $migration
* The Migration the plugin is being used in.
* @param \Drupal\migrate\MigrateLookupInterface $migrate_lookup
* The migrate lookup service.
* @param \Drupal\migrate\MigrateStubInterface $migrate_stub
* The migrate stub service.
* @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $migation_plugin_manager
* Migration plugin manager service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, MigrateLookupInterface $migrate_lookup, MigrateStubInterface $migrate_stub, MigrationPluginManagerInterface $migation_plugin_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $migrate_lookup, $migrate_stub);
$this->migrationPluginManager = $migation_plugin_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$migration,
$container->get('migrate.lookup'),
$container->get('migrate.stub'),
$container->get('plugin.manager.migration'),
);
}
/**
* {@inheritdoc}
*/
@ -41,8 +91,7 @@ class FarmMigrationGroupLookup extends MigrationLookup {
$lookup_migration_group_id = $this->configuration['migration_group'];
// Load all migrations.
$manager = \Drupal::service('plugin.manager.migration');
$migrations = $manager->createInstances([]);
$migrations = $this->migrationPluginManager->createInstances([]);
// Filter by group.
$group_migrations = [];

View file

@ -2,8 +2,18 @@
namespace Drupal\plan;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines the controller class for plans.
@ -13,6 +23,57 @@ use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
*/
class PlanStorage extends SqlContentEntityStorage {
/**
* The time service.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
/**
* Constructs an AssetStorage object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Database\Connection $database
* The database connection to be used.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* The cache backend to be used.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface $memory_cache
* The memory cache backend to be used.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle info.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
*/
public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityFieldManagerInterface $entity_field_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, MemoryCacheInterface $memory_cache, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityTypeManagerInterface $entity_type_manager, TimeInterface $time) {
parent::__construct($entity_type, $database, $entity_field_manager, $cache, $language_manager, $memory_cache, $entity_type_bundle_info, $entity_type_manager);
$this->time = $time;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entity_type,
$container->get('database'),
$container->get('entity_field.manager'),
$container->get('cache.entity'),
$container->get('language_manager'),
$container->get('entity.memory_cache'),
$container->get('entity_type.bundle.info'),
$container->get('entity_type.manager'),
$container->get('datetime.time'),
);
}
/**
* {@inheritdoc}
*/
@ -53,7 +114,7 @@ class PlanStorage extends SqlContentEntityStorage {
// If the state has changed to archived and no archived timestamp was
// specified, set it to the current time.
if ($new_state == 'archived' && $entity->getArchivedTime() == NULL) {
$entity->setArchivedTime(\Drupal::time()->getRequestTime());
$entity->setArchivedTime($this->time->getRequestTime());
}
// Or, if the state has changed from archived, set a null value.

View file

@ -3,25 +3,75 @@
namespace Drupal\farm_ui_action\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines farmOS action links.
*/
class FarmActions extends DeriverBase {
class FarmActions extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The entity type bundle info.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $entityTypeBundleInfo;
/**
* Constructs a FarmActions object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle info.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
$this->entityTypeManager = $entity_type_manager;
$this->moduleHandler = $module_handler;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$container->get('entity_type.manager'),
$container->get('module_handler'),
$container->get('entity_type.bundle.info'),
);
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
// Load the entity type manager.
$entity_type_manager = \Drupal::entityTypeManager();
// Load available entity types.
$entity_types = array_keys($entity_type_manager->getDefinitions());
$entity_types = array_keys($this->entityTypeManager->getDefinitions());
// Define the farmOS entity types we care about.
$farm_types = [
@ -40,13 +90,13 @@ class FarmActions extends DeriverBase {
// Generate a link to [entity-type]/add.
$name = 'farm.add.' . $type;
$entity_type_label = $entity_type_manager->getStorage($type)->getEntityType()->getLabel();
$entity_type_label = $this->entityTypeManager->getStorage($type)->getEntityType()->getLabel();
$this->derivatives[$name] = $base_plugin_definition;
$this->derivatives[$name]['title'] = $this->t('Add :entity_type', [':entity_type' => $entity_type_label]);
$this->derivatives[$name]['route_name'] = 'entity.' . $type . '.add_page';
// Add it to entity Views, if the farm_ui_views module is enabled.
if (\Drupal::moduleHandler()->moduleExists('farm_ui_views')) {
if ($this->moduleHandler->moduleExists('farm_ui_views')) {
$this->derivatives[$name]['appears_on'][] = 'view.farm_' . $type . '.page';
// If this is a log, also add it to view.farm_log.page_user.
@ -56,7 +106,7 @@ class FarmActions extends DeriverBase {
}
// Add it to farm.dashboard, if the farm_ui_dashboard module is enabled.
if (\Drupal::moduleHandler()->moduleExists('farm_ui_dashboard')) {
if ($this->moduleHandler->moduleExists('farm_ui_dashboard')) {
$this->derivatives[$name]['appears_on'][] = 'farm.dashboard';
}
@ -72,14 +122,14 @@ class FarmActions extends DeriverBase {
$this->derivatives[$name]['cache_tags'] = ['entity_bundles'];
// Add it to entity bundle Views, if the farm_ui_views module is enabled.
if (\Drupal::moduleHandler()->moduleExists('farm_ui_views')) {
if ($this->moduleHandler->moduleExists('farm_ui_views')) {
$this->derivatives[$name]['appears_on'][] = 'view.farm_' . $type . '.page_type';
$this->derivatives[$name]['bundle_parameter'] = 'arg_0';
}
// Generate links to [entity-type]/add/[bundle]?asset=[id] on asset pages.
if ($type == 'log') {
$bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('log');
$bundles = $this->entityTypeBundleInfo->getBundleInfo('log');
foreach ($bundles as $bundle => $bundle_info) {
$name = 'farm.asset.add.' . $type . '.' . $bundle;
$this->derivatives[$name] = $base_plugin_definition;

View file

@ -16,7 +16,7 @@ class ErrorPageController extends Http4xxController {
$output = parent::on403();
// If the user is already logged in, return.
if (!\Drupal::currentUser()->isAnonymous()) {
if (!$this->currentUser()->isAnonymous()) {
return $output;
}

View file

@ -3,15 +3,44 @@
namespace Drupal\farm_ui_views\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides task links for farmOS Logs Views.
*/
class FarmLogViewsTaskLink extends DeriverBase {
class FarmLogViewsTaskLink extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a FarmActions object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$container->get('entity_type.manager'),
);
}
/**
* {@inheritdoc}
*/
@ -19,7 +48,7 @@ class FarmLogViewsTaskLink extends DeriverBase {
$links = [];
// Add links for each bundle.
$bundles = \Drupal::service('entity_type.manager')->getStorage('log_type')->loadMultiple();
$bundles = $this->entityTypeManager->getStorage('log_type')->loadMultiple();
foreach ($bundles as $type => $bundle) {
$links['farm.asset.logs.' . $type] = [
'title' => $bundle->label(),

View file

@ -2,7 +2,10 @@
namespace Drupal\farm_ui_views\Plugin\Derivative;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\views\Plugin\Derivative\ViewsMenuLink;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides menu links for farmOS Views.
@ -22,6 +25,36 @@ class FarmViewsMenuLink extends ViewsMenuLink {
*/
protected string $entityType;
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a FarmActions object.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $view_storage
* The view storage.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
*/
public function __construct(EntityStorageInterface $view_storage, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($view_storage);
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$container->get('entity_type.manager')->getStorage('view'),
$container->get('entity_type.manager'),
);
}
/**
* {@inheritdoc}
*/
@ -29,7 +62,7 @@ class FarmViewsMenuLink extends ViewsMenuLink {
$links = [];
// Get the entity type definition. Bail if invalid.
$entity_type_definition = \Drupal::service('entity_type.manager')->getDefinition($this->entityType, FALSE);
$entity_type_definition = $this->entityTypeManager->getDefinition($this->entityType, FALSE);
if (empty($entity_type_definition)) {
return $links;
}
@ -38,7 +71,7 @@ class FarmViewsMenuLink extends ViewsMenuLink {
$bundle_entity_type = $entity_type_definition->getBundleEntityType();
// Load all available bundles for the entity type.
$bundles = \Drupal::service('entity_type.manager')->getStorage($bundle_entity_type)->loadMultiple();
$bundles = $this->entityTypeManager->getStorage($bundle_entity_type)->loadMultiple();
// Add links for each bundle.
foreach ($bundles as $type => $bundle) {

View file

@ -4,4 +4,4 @@ services:
arguments: [ 'farm_update' ]
farm.update:
class: Drupal\farm_update\FarmUpdate
arguments: [ '@logger.channel.farm_update', '@module_handler', '@entity_type.manager', '@config_update.config_diff', '@config_update.config_list', '@config_update.config_update' ]
arguments: [ '@logger.channel.farm_update', '@module_handler', '@entity_type.manager', '@config.factory', '@config_update.config_diff', '@config_update.config_list', '@config_update.config_update' ]

View file

@ -5,6 +5,7 @@ namespace Drupal\farm_update;
use Drupal\config_update\ConfigDiffer;
use Drupal\config_update\ConfigListerWithProviders;
use Drupal\config_update\ConfigReverter;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
@ -38,6 +39,13 @@ class FarmUpdate implements FarmUpdateInterface {
*/
protected $entityManager;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The config differ.
*
@ -68,6 +76,8 @@ class FarmUpdate implements FarmUpdateInterface {
* The module handler.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
* The entity type manager.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\config_update\ConfigDiffer $config_diff
* The config differ.
* @param \Drupal\config_update\ConfigListerWithProviders $config_list
@ -75,10 +85,11 @@ class FarmUpdate implements FarmUpdateInterface {
* @param \Drupal\config_update\ConfigReverter $config_update
* The config reverter.
*/
public function __construct(LoggerInterface $logger, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_manager, ConfigDiffer $config_diff, ConfigListerWithProviders $config_list, ConfigReverter $config_update) {
public function __construct(LoggerInterface $logger, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, ConfigDiffer $config_diff, ConfigListerWithProviders $config_list, ConfigReverter $config_update) {
$this->logger = $logger;
$this->moduleHandler = $module_handler;
$this->entityManager = $entity_manager;
$this->configFactory = $config_factory;
$this->configDiff = $config_diff;
$this->configList = $config_list;
$this->configUpdate = $config_update;
@ -135,7 +146,7 @@ class FarmUpdate implements FarmUpdateInterface {
$exclude_config = $this->moduleHandler->invokeAll('farm_update_exclude_config');
// Load farm_update.settings to get additional exclusions.
$settings_exclude_config = \Drupal::config('farm_update.settings')->get('exclude_config');
$settings_exclude_config = $this->configFactory->get('farm_update.settings')->get('exclude_config');
if (!empty($settings_exclude_config)) {
$exclude_config = array_merge($exclude_config, $settings_exclude_config);
}