Questions tagged [symfony-services]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0
votes
2answers
46 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\...
0
votes
0answers
25 views

Class not found in Drupal\Component\DependencyInjection\Container->createService()

I have a custom module (srs) that has an event subscriber as a service. That event subscriber class uses other custom helper class files after it catches the event. I tried to register the helper ...
0
votes
0answers
36 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
1answer
39 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
16 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
115 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
0answers
35 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
1answer
38 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
1answer
39 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
100 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
0answers
71 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 ...
2
votes
2answers
67 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
160 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 ...
3
votes
0answers
240 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
41 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
58 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
67 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
2answers
73 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
2answers
354 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
0answers
78 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
44 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
367 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
2answers
313 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 ...
2
votes
0answers
383 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
1answer
176 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
75 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 ...
2
votes
2answers
4k 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
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 ...
3
votes
1answer
275 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
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\...
1
vote
1answer
140 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
0answers
851 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 ...
12
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 ...
5
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 ...
1
vote
1answer
60 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 ...
5
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 ...
12
votes
4answers
16k 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 ...
2
votes
0answers
87 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 ...
4
votes
2answers
514 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 ...
2
votes
1answer
376 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 ...
1
vote
2answers
3k 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
1answer
4k 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 ...
8
votes
7answers
5k views

Get the list of available services

Is there a way to get the list of the available services in Drupal 8?
1
vote
2answers
406 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....
0
votes
1answer
248 views

Service event_subscribe before path_processor_inbound

I have ha service with tag: event_subscribe (KernelEvents::REQUEST) with priority 30 and in the same module a service path_processor_inbound with priority 29 The problem is: in event_subscribe ...
0
votes
1answer
415 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/...
8
votes
1answer
5k 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() { $...
1
vote
1answer
42 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
1answer
478 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\...
2
votes
1answer
354 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 ...