Skip to main content
26 votes

Correct way to call a service in a controller

There are 3 questions here: How to create a controller? How to create a service? How to use services in a controller? 1. Create a controller 1.1 Add a controller class - you did it. For others ...
zendka's user avatar
  • 421
23 votes
Accepted

How do I use dependency injection on \Drupal::moduleHandler() to a Controller?

In a controller you inject services by using the service container. For example ModuleHandler: namespace Drupal\mymodule\Controller; use Drupal\Core\Controller\ControllerBase; use Symfony\Component\...
4uk4's user avatar
  • 99.8k
9 votes
Accepted

What is the difference between \Drupal::request()->getSession() and \Drupal::service('user.private_tempstore')?

The main difference is that session data is stored in the database table sessions and PrivateTempStore in key_value_expire. Session data is preloaded in memory, private tempstore data only when ...
4uk4's user avatar
  • 99.8k
7 votes

CSRF validation failed issue on custom services!

You can check workflow steps First Time for login POST /services/session/token to retrieve CSRF Token POST myendpoint/system/connect with X-CSRF-Token header IF not Logged In Try Log in /myendpoint/...
visabhishek's user avatar
  • 1,346
6 votes

How do I use the global class \Drupal static methods to access services?

\Drupal::service('config.factory') and \Drupal::configFactory() are equivalent; the second is just shorter. There are many, many services provided by core, and only a few of them have a dedicated ...
Jaypan's user avatar
  • 17.6k
5 votes
Accepted

CSRF validation failed issue on custom services!

In the POSTMAN you must be sending X-CSRF-Token with the API call, while you might be missing the same while doing API call from APP. X-CSRF-Token is required when you have session authentication ...
arpitr's user avatar
  • 7,836
4 votes
Accepted

Get all fields for a content type using REST

The /entity/entity_form_display/{entity_form_display} should works. Example: /entity/entity_form_display/node.article.default?_format=json It will return json like this: { "uuid": "058dd73d-366d-...
Cong Nguyen's user avatar
4 votes
Accepted

Make PDF,XLSX,DOCX,PPTX output for drupal REST server

Yes it's definitely possible, but you'll need to write a formatter class for each type of file as the bundled REST server only has support for XML, JSON, JSONP, serialized PHP, bencode, and optionally ...
Clive's user avatar
  • 167k
4 votes
Accepted

How do I handle exceptions in a service class?

When using unqualified class names like Exception and Error, and in the absence of a use statement to qualify the names, PHP assumes those classes are in the "current" namespace. The ...
anonymous's user avatar
  • 2,137
4 votes
Accepted

How to check config setting in custom service class with dependency injection?

Make sure you inject config.factory and then you need to use: $this->config ->get('my_custom_module.settings') ->get('display_debugging_info'); You can see that Drupal::config() is: ...
sonfd's user avatar
  • 7,921
3 votes

Using Services, why is the resource name appended to the endpoint?

See mradcliffe's comment for a good explanation of the difference between an endpoint and a resource, understanding that will make it clear why the Services module does things the way it does. Why is ...
Clive's user avatar
  • 167k
3 votes

User login API for social login from mobile apps

I was also looking for functionality that you needs, but after wards i tried to implement service for social sign in using Hybridauth module and i succeed. Try this https://github.com/rathodhiren9/...
hiren rathod's user avatar
3 votes

what is the use of universal unique identifier ,why its so much important in deployment?

UUID is used to identify the same objects (like nodes, taxonomy_terms and users) across environments. All of these has an ID, for nodes it's the nid (node id), but that is not reliable to use across ...
googletorp's user avatar
  • 35.4k
3 votes
Accepted

How to get all terms with services module?

You need to grant permission for Perform unlimited index queries at admin/people/permissions. Alternatively, you can set this in settings.php: $conf['services_taxonomy_term_index_page_size'] = 200;
tyler.frankenstein's user avatar
3 votes
Accepted

Event does not trigger each time I load a page

This look like it is the module "Internal Page Cache", which caches pages for anonymous users. This cache runs before any event as middleware. In your case there is no need for this module and you ...
4uk4's user avatar
  • 99.8k
3 votes
Accepted

How to convert objects into array using serialization API?

I tried serializer.normalizer.content_entity but got no success in normalizing the (node) revision entity, or even node entity for that matter. It always resulted in the following error: PHP Fatal ...
AjitS's user avatar
  • 10.9k
3 votes
Accepted

How to securely authenticate content embeded in another site?

1- build the content structure well in your first site and create a view from admin/structure/views/add that return the data needed to the second site which acts like a client consuming your first ...
Mohammed Gomma's user avatar
3 votes
Accepted

How do I use a "contains" operation as restful contextual filter for text fields

When you expose a filter, you can set a filter identifier. This can be used to set the filter value via the URL, E.g.: /search?keywords=foo or /search?keyworks=foo+bar. Note that if you need to ...
CG Monroe's user avatar
  • 3,056
3 votes

Which is better way of injecting service to controller with __construct or $instance = parent::create($container)

TL;DR: The second way is better. The constructor method __construct() is considered internal, you can not (savely) overwrite it. E.g. the number or type of mandatory function parameters might change. ...
Hudri's user avatar
  • 5,500
3 votes
Accepted

Return AccessDenied for custom page

In your route, instead of _permission, you can use a custom access callback: _custom_access: Drupal\[MODULE]\Controller::accessCallback() Then you can return an AccessResult from the access callback: ...
Jaypan's user avatar
  • 17.6k
3 votes

Change Priority of ServiceProvider class

It seems like you are in control of the fully-qualified class name. It doesn't matter how often it is altered, the latest change counts. Which alterations your custom class contains depends on the ...
4uk4's user avatar
  • 99.8k
2 votes

Consuming RESTful web services

From the HTTP Client Manager module page: Http Client Manager introduces a new Guzzle based plugin which allows you to manage HTTP clients using Guzzle Service Descriptions via JSON files, in a ...
Sinan Erdem's user avatar
2 votes

How to use Services Basic Authentication?

Once the module is enabled go to Structure > Services Select your Endpoint and click Edit Use a tool such as https://addons.mozilla.org/fr/firefox/addon/restclient/ to test your client: Note: I've ...
Artur Kedzior's user avatar
2 votes

How do I enable CORS on Service endpoint?

I had to enable headers on my ubuntu server: sudo a2enmod headers Also, for the CORS settings I used: *|http://myappURL-that-gets-data-from-drupal
knice's user avatar
  • 419
2 votes
Accepted

is it possible to edit default output of services module?

Using the Services API this is possible: http://cgit.drupalcode.org/services/tree/docs/services.alter.api.php?h=7.x-3.x /** * Implements hook_services_request_postprocess_alter(). */ function ...
tyler.frankenstein's user avatar
2 votes

How do I create and synchronize product fields with entity fields on another site

I'll start with saying, you should throw your "no custom coding" requirement out. That's not to say it's not possible, but it's going to require you stitching a patchwork of solutions that might drive ...
Shawn Conn's user avatar
  • 26.9k
2 votes

Ionic Framework won't authenticate in browser (works in emulator)

Incase someone faces a similar issue, I managed to find a fix by: Setting up an ionic proxy see: http://blog.ionic.io/handling-cors-issues-in-ionic/ Sending the Cookie with the headers as ...
williamsowen's user avatar
2 votes

How do I create and synchronize product fields with entity fields on another site

Cross-site sync in Drupal 7 is non-trivial (there's a large project happening to make it easy in Drupal 8...some day), and since everything can have customizations general purpose tools have to be so ...
acrosman's user avatar
  • 2,728
2 votes

Forgot password REST services_token

In Drupal 8 you can reset it by curl POST 'http://YOUR_SITE.URL/user/password?_format=json' \ --header 'Content-Type: application/json' \ --header 'Content-Type: application/json' \ --data-raw '{ ...
Yuseferi's user avatar
  • 22k
2 votes

Services Download API

You can send an image through resources using base64_encode(), or _file_resource_retrieve(). I don't remember the specifics of it all, but here is how I did it for Services in a project last year. ...
Jaypan's user avatar
  • 17.6k

Only top scored, non community-wiki answers of a minimum length are eligible