Skip to main content

Why drupal is not fully object oriented ?

You can see module hooks are still in a procedural way to  utilize a dependent service 

That's why we have a static  Drupal Class, it can get a service in a procedural context.

How to access a service in module hooks?

Drupal::service('{service id}') or 

service accessors like Drupal::request(), Drupal::currentUser(), Drupal::entityManager().

It is providing a uniform way of getting any services inside hooks and it eases the transition from procedural code to injected oops code.

How a container get services in drupal? 

The container is built by the kernel  and passed to the static Drupal Class and the container gets all the services from the system by the below methods

   1. \Drupal\Core\CoreServiceProvider

   2.  the service providers of enabled modules 

   3. any other service providers defined in $GLOBALS['conf']['container_service_providers'].

How you can improve the dependent service access from module ? 

function hook_do_stuff() {
$lock = lock()
->acquire('stuff_lock');
// ...

// Correct procedural code.
function hook_do_stuff() {
$lock = \Drupal::lock()
->acquire('stuff_lock');
// ...
}
// The preferred way: dependency injected code.
function hook_do_stuff() {
// Move the actual implementation to a class and instantiate it.
$instance = new StuffDoingClass(\Drupal::lock());
$instance
->doStuff();
// Or, even better, rely on the service container to avoid hard coding a
// specific interface implementation, so that the actual logic can be
// swapped. This might not always make sense, but in general it is a good
// practice.
\Drupal::service('stuff.doing')
->doStuff();
}
interface StuffDoingInterface {
public function doStuff();
}
class StuffDoingClass implements StuffDoingInterface {
protected $lockBackend;
public function __construct(LockBackendInterface $lock_backend) {
$this->lockBackend = $lock_backend;
}
public function doStuff() {
$lock = $this->lockBackend
->acquire('stuff_lock');
// ...
}





Comments

Popular posts from this blog

Process of page request to response in Drupal 8 - Part 1

For every drupal developer is it necessary to know the under the hood process of how drupal internally works and some of them may already know  page rendering process of drupal 7. Lets see the Process of User Request to Response in  two parts: PART 1: Step 1:  The user make a request in the browser in the form of url for example: www.example.com Step 2:  The browser will pass the request to the Web server where the website  resides(Here Drupal makes website...) Step 3: In drupal , every request is handled via index.php file ,while digging into index file we may see the following lines as shown in the image. Note:  Symfony is a playing a major role  in  drupal 8 and drupal 8 follows the object oriented style of programming .. Components used from symfony in drupal 8.  So as you see , there is a two use statements  1.use Drupal\Core\DrupalKernel 2.use Symfony\Component\Ht...

Things You Must Know Before Trying Forms In Drupal 8.

 You Should Invest In Forms                      What does your customer think?  What do they looking for? will decide the success of any business.    In a digital marketing, Most of the customer requirement is gathered through forms. So its   essential  to spend some  time with forms Example : a user can search a particular product using the search form on an e-commerce website. Form API in Drupal 8          As we know already Drupal use forms for most of the administrative site building. Drupal 8 follows object-oriented style of programming with the integration of symfony . Based on the purpose of the form, it can inherit the basic properties and methods of the below classes. 1.Config Base 2.FormBase 3.ConfirmBase   Note : Routing configuration is done through the yml files in drupal 8 When the user hits the va...