Questions tagged [symfony-services]

The tag has no usage guidance.

1
vote
0answers
67 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
28 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
2answers
41 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
90 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
166 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
58 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
68 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
2answers
1k 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
618 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
216 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
851 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
99 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
573 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 ...
11
votes
2answers
2k 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 ...
4
votes
1answer
971 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
51 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 ...
4
votes
1answer
926 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 ...
7
votes
4answers
9k 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
82 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 ...
3
votes
2answers
348 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
1answer
257 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 ...
0
votes
2answers
2k 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
votes
1answer
3k 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 ...
4
votes
3answers
3k 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
207 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
182 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
346 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/...
6
votes
1answer
3k 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
38 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
374 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
232 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 ...
1
vote
1answer
47 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 ...
5
votes
1answer
527 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 ...
0
votes
2answers
140 views

Concept of services, tags, and arguments [closed]

I defined a service using the following code. services: redirect.repository: class: Drupal\redirect\RedirectRepository arguments: ['@entity.manager', '@database', '@config.factory'] ...
6
votes
6answers
4k 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\...
0
votes
0answers
271 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
300 views

how to load 2 services (or more) through dependency injection

if i have 1 service, i can load my service through dependency injection like: class MyForm extends FormBase { protected $coreService; /** * Class constructor. */ public function ...
1
vote
1answer
71 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
1k 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, ...
2
votes
2answers
1k 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
1answer
255 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 ...
3
votes
1answer
1k 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 ...
8
votes
1answer
315 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.