Questions tagged [symfony-services]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
16
votes
4answers
22k 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 ...
13
votes
2answers
3k 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 ...
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?
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\...
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() { $...
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.
7
votes
2answers
690 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 ...
7
votes
1answer
2k 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 ...
7
votes
1answer
2k views

How to inject a service in another service?

I am building a custom service, a membership service. This service can be called from a controller. But what I don't seem to get working, is to use another service, the QueryFactory, to use in that ...
6
votes
1answer
1k views

Why isn't my service found?

I'm trying to load a service into my Drupal module, but even after checking twice whether I did something wrong, I can't seem to find any error in my code as I did everything as the documentation said ...
5
votes
1answer
293 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
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())->...
3
votes
1answer
345 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....
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 ...
3
votes
1answer
354 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 ...
3
votes
0answers
511 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 ...
3
votes
0answers
558 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: ...
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 ...
2
votes
1answer
423 views

The path_processor_outbound service only work after clearing the cache

I tried overwrite all links from my site in Drupal 8 in accordance with a URL parameter. I use the service path_processor_outbound and implemented the processOutbound() method, extending the ...
2
votes
1answer
240 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 ...
2
votes
1answer
450 views

Inject a service inside a block

I need to inject a service inside a block. Since I need some logic before displaying the block, it feels dirty to do it inside the block itself, not to mention it's not actually related to the block ...
2
votes
1answer
400 views

Custom version of DateFormatter service

I want to create a slightly different version of the formatInterval function. Normally I do this: echo \Drupal::service('date.formatter')->formatInterval($seconds); I want the final string to say ...
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\...
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 ...
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'...
2
votes
0answers
97 views

Design help for serialized classes that need database access

I want to convert from \Drupal::database() to dependency injection. I'm not 100% certain how to move forward with a refactor I know I have to complete. Currently I have a series of custom classes ...
1
vote
2answers
4k views

Get state list of a country

In drupal 8 we can get country list using following code. $countries = \Drupal::service('country_manager')->getList(); But how can I programatically get the list of state of each country?
1
vote
2answers
448 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\...
1
vote
1answer
97 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
2answers
131 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
2answers
553 views

Service provider not loaded

I have: services: entity.query.external: class: Drupal\m_entities\Entity\Query\External\QueryFactory arguments: ['@plugin.manager.my_entity_storage_client', '@my_entity.storage_client....
1
vote
1answer
44 views

Using a Service to expose better getter methods for a Node instance

Currently we use a Service that takes an NodeInterface object as an argument and then gives back a value, i.e: public function iFullyBooked(NodeInterface $node) { $fully_booked_field = $node-&...
1
vote
2answers
342 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
vote
1answer
86 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....
1
vote
1answer
891 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','@...
1
vote
1answer
96 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 ...
1
vote
1answer
64 views

Can a Event Service be coupled to a route

I have created a custom service and have hooked into the "event_subscriber" via tags in my custom service.yml demo_api.exception_handler_subscriber: class: Drupal\demo_api\Response\Handler ...
1
vote
1answer
521 views

How to resolve my warning in my custom service?

I am getting the following error: Warning: Missing argument 1 for Drupal\custom_forms\Service\CustomFormsService::__construct(), called in /drupal/docroot/core/lib/Drupal/Component/...
1
vote
1answer
557 views

Create service with argument

I have class like this: <?php namespace Drupal\mymodule; use Drupal\comment\CommentInterface; use Drupal\comment\CommentManagerInterface; use Drupal\comment\CommentStorage; use Drupal\Core\...
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 ...
1
vote
1answer
130 views

How to properly inject services into other services

I'm trying to figure out how to inject Drupal services into a custom service class. The class in question is my MenuService which builds an associative array of menu items which will be encoded to ...
1
vote
1answer
79 views

Service function is not calling

I am trying to validate a key, which I am passing as url argument using service. But service function is not calling. It's giving "Access denied" error. Am I missing something? My sample code is: ...
1
vote
2answers
3k views

how to access configuration module from service

I have module config, something like: modulename.config.yml: modulename: core_address: 107.101.xx.yy core_port: 153759 core_user: superuser core_pass: superpass And I have module service, ...
1
vote
1answer
73 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 ...
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 {}...
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 ...
1
vote
0answers
26 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 ...
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 ...
1
vote
0answers
142 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 ...