Questions tagged [symfony-services]
The symfony-services tag has no usage guidance.
79
questions
0
votes
0
answers
26
views
How to change order items in autocomplete entity?
I override alter autocomplete in my custom module but I don't know how I can change the display order (DESC or ASC)
<?php
namespace Drupal\alter_entity_autocomplete;
use Drupal\Component\Utility\...
2
votes
1
answer
126
views
addViolation() is deprecated: how to replace it?
How to adapt the following code to avoid the deprecation of addViolation() according to https://www.drupal.org/node/3238432 ?
class MyValidator extends ConstraintValidator {
public function ...
5
votes
1
answer
65
views
Extending contrib module filelog service class displays PSR implementation error
I wanted to override the logger.filelog service from filelog module to make use of my own parser.
An error occurred through drush cim and drush cr when the new custom module that overrides the service ...
1
vote
0
answers
78
views
Injecting service into another service does not work
I have to override pagecache service in order to have some custom validation logic to be executed for anonymous users. The issue is that I have the domain module's service domain.negotiator to be ...
4
votes
1
answer
101
views
non-existent service, yet cache is cleared and module is enabled
I'm trying to access a Drupal service for normalizing content, yet the service is non-existent, even though the module "hal" is enabled and the service is defined. How can I get the service &...
0
votes
0
answers
18
views
How do I configure Scout APM?
Their docs have setup instructions for Symfony, but I'm not sure how they translate to Drupal: https://scoutapm.com/docs/php/symfony. Could anybody provide some insight on how the xml/bundle ...
1
vote
2
answers
137
views
Injecting a NEW instance of a service into another service class
I have a service (for sending error alerts) which is used by multiple other services.
The alert service is injected into those classes via the relevant services.yml files and through their ...
0
votes
1
answer
46
views
Site error on Drupal 9 on Acquia Dev Desktop [closed]
I am working on the Packt book Drupal 9 Module Development
I finished the chapter 3 Here is the Github Link to the code
When I call an url of admin part of my Drupal (I wanted to clear the cache) I ...
1
vote
1
answer
147
views
Return a Closure from a Factory
I have services that depend on request information. I'm trying to create a Factory Factory which will have the Dependency Injection container returns a factory (an anonymous function) that will create ...
1
vote
2
answers
301
views
How to alter the parameters in the core.services.yml file programatically?
I need to modify the filter_protocols parameter inside the core.services.yml file programmatically to add a new protocol. I know how to alter a service but I don't find any information about how to ...
1
vote
1
answer
85
views
New module service is dependency on old module service - The service has a dependency on a non-existent service
I have a new module moduleB. It defines a service that is used in the dependency injection of moduleA.
moduleA is already enabled, its changes are just to the services definition. The info.yml has ...
1
vote
1
answer
58
views
Missing proxy classes when container compiles custom ExtensionPass
Context
I try to find a way to integrate Webpack Encore into Drupal. Since we can't use Symfony Bundles in Drupal I want to port the bundle to a custom Drupal module.
What I have done so far
To ...
1
vote
1
answer
207
views
How to inherit base class dependencies without redefining theme in child class service arguments?
The current system
I have a set of services classes that extends the same base class ex:
class NodeHandler extends ContentEntityHandlerBase {}
class ParagraphHandler extends ContentEntityHandlerBase {}...
0
votes
1
answer
267
views
Are service autowiring and class autoresolving not anymore supported?
In Drupal 8, I needed to define services via the mymodule.services.yml file only, if I wanted to use them via the service container. I could also autowire them and the classes were automatically ...
1
vote
0
answers
63
views
Database password revealed in PHP error_log?
Noticed some errors while updating to Drupal 8.9.11. As I'm reviewing I come across the following. Replaced the actual password with PASSWORD below.
Is this intentional or a bug? I can't see why the ...
2
votes
1
answer
209
views
How to inject an object into a service?
I need to do a couple of things:
Inject guzzle's httpclient into class A
Inject class A into my service
What I've tried so far:
services:
my_module.field_fetcher:
class: Drupal\my_module\...
1
vote
2
answers
960
views
How do I pass dynamic parameters to a service?
Is it possible to pass dynamic parameters when calling a service?
mymodule.services.yml
services:
my_module.default:
class: Drupal\services\DefaultService
arguments: ['@entity_type.manager']
...
-1
votes
1
answer
47
views
How to get $_POST which has blank parameters [closed]
I understand that as per new rules, post request in drupal should be replaced with
$stack->getCurrentRequest()->request;
But I need to know what is the replacement for $_POST which does not ...
0
votes
1
answer
41
views
What is the syntax for using a contrib module's service from within a hook implementation?
I'm trying to get a list of objects from AWS using the S3fsService from the S3 File System module from within a hook_cron() implementation. The following code works:
// Fetch list of files in S3.
$...
0
votes
2
answers
257
views
Drupal 8 service dependency injection [closed]
I am trying to build a service that will leverage the Pathauto service so my class looks like this.
namespace Drupal\some_system;
use Drupal\pathauto\PathautoGenerator;
use Drupal\pathauto\...
-1
votes
1
answer
598
views
Web service APIs returns HTML content instead of JSON response
I have used Drupal 7 before and the web services were working perfectly fine. I recently started working on Drupal 8 (8.8 to be precise), and the login service is not working properly. When I do not ...
0
votes
1
answer
162
views
Replace \Drupal::service with Dependency injection in FieldWidget class
I have created an ajax callback inside a custom Field Widget plugin. In the Ajax callback I have called a custom service using
$service = \Drupal::service('service.name')
I am using phpmd to clean ...
1
vote
0
answers
48
views
Can we have multiple Normalizers in REST API?
I have a requirement of exposing multiple content type data via different REST end point. I am using Normalizers to create an array as expected. For one content type it works perfect but when i try to ...
2
votes
1
answer
382
views
$base_root problem
In /core/lib/Drupal/Core/DrupalKernal.php the $base_root shows incorrect value:
Line 1113
protected function initializeRequestGlobals(Request $request) {
global $base_url;
// Set and ...
0
votes
0
answers
428
views
How to override a service provided by a contrib module?
I need to override Validator class from Telephone Validations contrib module.
I tried the both static and dynamic methods described here:
https://www.drupal.org/docs/8/api/services-and-dependency-...
0
votes
1
answer
291
views
How to override a service
I am trying to override a service by using a ServiceProvider Class
class MyModuleServiceProvider extends ServiceProviderBase
{
public function alter(ContainerBuilder $container)
{
// ...
1
vote
1
answer
68
views
Difference between creating a Rest endpoint and a route&controller with jsonResponse
What is the difference between creating a Rest plugin with the Rest API and creating a route/controller that returns a jsonResponse ?
In which case i have to create a custom route/controller with a ...
0
votes
1
answer
350
views
Symfony's getQueryString method doesn't return modified query parameters
Symfony's getQueryString method doesn't return modified query parameters. So how should someone go about getting a modified query parameter string?
Example code:
$request = \Drupal::request();
...
2
votes
2
answers
86
views
What good is the create() method? [closed]
Most classes are instantiated using create($container) and __construct(...$services) in conjunction, it seems that the more efficient workflow would be to simply pass the container to the constructor ...
6
votes
1
answer
551
views
What is the right way of loading a service?
when trying to use a service in drupal actually there is 3 ways , what is the preferred way to use a service and why :
passing dependencies directly through the constructor:
public function ...
4
votes
0
answers
694
views
How can I use TwigExtension from an existing symfony bundle?
UPDATED
Context
I'm using Symfony Webpack Encore to take care of all the JS and CSS postprocessing, transpiling and so on...
There are different ways to implement webpack, even a module, Webpack ...
1
vote
1
answer
147
views
Set parent service when registering a service in a ServiceProvider
I want to create dynamic services, therefore I added a ServiceProvider class to my module.
I have created a base service that my other services will have as parent.
When doing this in my_module....
0
votes
1
answer
80
views
Why all core services cannot be registered in core.services.yaml?
DrupalKernel
Building of Dependency Injection Container and registration of Service Providers Core provides the CoreServiceProvider, which, in addition to registering any core services that cannot ...
1
vote
0
answers
240
views
Get all services that are tagged with a specific tag in *.services.yml
Is there an easy (supported) way to programatically get all services in Drupal 8 that are tagged with a specific tag?
For example getting all services that are tagged with "breadcrumb_builder" tag
...
0
votes
2
answers
175
views
How add pager with external request
After many search and read posts, I'm still stuck with my code. Let me explain : I created a custom module who display newsletters from Mailjet and old newsletters comming from drupal. To avoid the ...
1
vote
2
answers
591
views
How can I make a response cachable by browsers?
I am working on a Drupal Controller implementation that would return a generated image as response. I used Symfony's Response and I was trying to add ETag header to it:
use Drupal\Core\Controller\...
2
votes
0
answers
84
views
Why is @? used when referring router.request_context?
In core.services.yml we have:
calls:
- [setContext, ['@?router.request_context']]
But router.request_context is also defined in core.services.yml (and it depends on request_stack alone which doesn'...
1
vote
1
answer
1k
views
Problem in dependency injection in drupal custom module service
I face with a problem in defining service in drupal 8.0
my service is :
services:
tracking.middleware:
class: Drupal\mkt_tracking_v2\Middleware\TrackingMiddleware
arguments: ['@kernel','@...
0
votes
2
answers
767
views
Add parameter to the URL based on a field value
Is it possible to append a campaign tracking code to the requested URL basing on a specific field value on the linked node? I'm trying to use a path_processor_outbound service, but it doesn't work ...
3
votes
0
answers
815
views
How to get environment variables in a service container? [closed]
According to the Symfony documentation, env() parameters were introduced in version 3.2, allowing to use environment variables in service container configuration:
# app/config/config.yml
doctrine:
...
0
votes
1
answer
293
views
Instantiate Controller with Interface not concrete class [closed]
As background, I am rebuilding some old code and want to wrap all the curl based requests in an interface (HttpRequestInterface) so I can swap that out later without having to write all the api calls ...
1
vote
1
answer
130
views
How to define event listener in the service?
In Drupal 8 is possible to use event subscriber that is the separate class that defines the callback that should be called on the defined event. See Event Systems Overview & How To Subscribe To ...
3
votes
2
answers
7k
views
How can I log(watchdog)?
In D7 is used to log using Watchdog function. In D8, I see its gone and I should use something called the logging API.
What I a trying to do:
I have created a menu/route, mysite.com/logmessage
When ...
0
votes
1
answer
2k
views
InvalidArgumentException: Cannot redirect to an empty URL. in Symfony
Pages are not shwoing up. They display the follwoing error:
InvalidArgumentException: Cannot redirect to an empty URL. in Symfony\Component\HttpFoundation\RedirectResponse->setTargetUrl() (line 86 of ...
3
votes
1
answer
391
views
How can I inject a different service in an existing one, i.e. exchange one already injected service by an own one?
The basic authentication provider service uses the user.auth service:
basic_auth.authentication.basic_auth:
class: Drupal\basic_auth\Authentication\Provider\BasicAuth
arguments: ['@config....
0
votes
2
answers
2k
views
Dependency injection or \Drupal class methods?
Suppose I have an ExampleService class and I need a service (current_user) to process my logic. So I have implemented my service like this.
The way 1: DI standard Drupal
namespace Drupal\...
1
vote
1
answer
174
views
How to instantiate a service inside ServiceProvider?
I need to instantiate a service inside ServiceProvider.
At that point the container is not built.
Any ideas or useful links?
<?php
namespace Drupal\my_module;
use Drupal\Core\...
1
vote
0
answers
1k
views
How to understand and fix a Reflection does not exist error?
I am trying to get my head around using EventSubscriber for adding HTTP Headers. In D7 I would have simply used
drupal_add_http_header('Content-Type', 'application/javascript');
But I see that this ...
14
votes
2
answers
4k
views
When should I create a service or a utility function?
I had this question in mind along all the last week: When I should create a service or an utility function?
In the Drupal Core we have both Services and Utility functions, but I can't find the ...
8
votes
1
answer
3k
views
how to unit test an http client service with DI in drupal 8?
I have a service the consumes data from an external source. I would like to unit test the service and I am not sure how to get the test set up. To start with I would like to just test 1 method. Here ...