Questions tagged [symfony-services]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1
vote
1answer
95 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
1answer
5k views

How do I pass parameters to a service?

How can parameters be passed to service? I have a class that I would like to change to a service and load other services from the container. What I don't understand is how to deal with other ...
0
votes
2answers
549 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 ...
7
votes
2answers
688 views

When should I use DependencySerializationTrait?

I have noticed that some classes contain the following line: use DependencySerializationTrait; I know this trait is used for serialization, but I don't get when to use it and what kind of classes ...
1
vote
2answers
126 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 ...
0
votes
1answer
1k 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 ...
1
vote
1answer
72 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
1answer
36 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 ...
3
votes
0answers
508 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
1answer
51 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
1answer
158 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 ...
0
votes
1answer
229 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 ...
1
vote
0answers
38 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 ...
3
votes
1answer
347 views

How to implement StackMiddleware for a redirection correctly?

I'm working on a redirection (for unauthenticated users) for my Drupal 8 site. Currently, the redirection works however, the redirection persists even after user is authenticated. I found this article ...
9
votes
6answers
8k views

How to get ip address?

I used the following code: services: foo: class: Drupal\MYMODULE\Foo arguments: ['@request'] In MYMODULE/src/Foo.php namespace Drupal\MYMODULE; use Symfony\Component\HttpFoundation\...
2
votes
1answer
84 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
2answers
328 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
1answer
46 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
1answer
23 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
2answers
134 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\...
11
votes
7answers
7k views

Get the list of available services

Is there a way to get the list of the available services in Drupal 8?
8
votes
1answer
6k views

“\Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container.”

I am attempting to write a PHPUnit test for a service of mine. My test looks like the following. class CustomServiceTest extends UnitTestCase { public function testDefaultBehavior() { $...
0
votes
1answer
96 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
0answers
25 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
1answer
237 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 ...
1
vote
1answer
84 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
1answer
130 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) { // ...
0
votes
2answers
124 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 ...
0
votes
0answers
199 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-...
3
votes
0answers
552 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: ...
1
vote
1answer
47 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
1answer
206 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
2answers
71 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 ...
5
votes
1answer
285 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 ...
0
votes
2answers
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\...
0
votes
1answer
73 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
0answers
140 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 ...
1
vote
2answers
445 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
2answers
5k 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 ...
16
votes
4answers
21k views

How do I set up CORS?

Drupal 8 has RESTful web service in-built in core and since 8.2 we don't need cors module. Now to use services we just enable and configure default.service.yml file as specified in here However, I ...
4
votes
2answers
2k views

How do I build Breadcrumbs for a certain node?

In a node preprocess hook I am getting breadcrumbs for the currently viewed node by doing the following: $vars['breadcrumb'] = Drupal::service('breadcrumb') ->build(Drupal::routeMatch())->...
2
votes
0answers
83 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'...
0
votes
0answers
45 views

Update Symfony before Drupal core?

Right now there are some security updates for Symfony: curl -sS https://get.symfony.com/cli/installer | bash echo y | ~/.symfony/bin/symfony self:update ~/.symfony/bin/symfony security:check Symfony ...
1
vote
1answer
885 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
0answers
359 views

Class not loading

For my module cloudinary_storage_db I have a cloudinary_storage_db.services.yml with the following: services: cloudinary_storage_db: class: Drupal\cloudinary_storage_db\CloudinaryStorageDb ...
0
votes
1answer
244 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
1answer
94 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
1answer
2k views

Is it possible to pass configuration values as arguments to services?

I'm writing a module that makes calls to a Web service to read and store data. Here's my services.yml file: services: example.connection: class: Drupal\Core\Http\Client I've already a ...
1
vote
0answers
972 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 ...
8
votes
1answer
392 views

How to change core's default services

Dependency Injection is added to core, but there's no documentation to tell how developer can alter the default services. For example, I would like to change cache.bootstrap class to APC Caching.