Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

Every time I read about how to convert code from D7 to D8 is seems like the D8 version is longer.

For example drupal_goto was:

drupal_goto('user');

and the D8 equivalent is:

return new RedirectResponse(\Drupal::url('user.page'));

And an example hook_init in D7:

function mymodule_init() {
  if (!empty($_GET['redirect-me'])) {
    drupal_goto('http://example.com/');
  }
}

And the equivalent is D8 is:

mymodule.services.yml

services:
  mymodule_event_subscriber:
    class: Drupal\mymodule\EventSubscriber\MymoduleSubscriber
    tags:
      - {name: event_subscriber}

and then also:

namespace Drupal\mymodule\EventSubscriber;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MymoduleSubscriber implements EventSubscriberInterface {
  public function checkForRedirection(GetResponseEvent $event) {
    if ($event->getRequest()->query->get('redirect-me')) {
      $event->setResponse(new RedirectResponse('http://example.com/'));
    }
  }
  /**
   * {@inheritdoc}
   */
  static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array('checkForRedirection');
    return $events;
  }
}

This is only a couple of examples but it seems that to achieve anything is D8 is going to take far more code.

  • Is this actually the case or am I misinterpreting whats happening.
  • If this is the case then what is the advantage?
  • Would it be possible / desirable to use lots of wrapper functions to reduce the amount of code to achieve simple tasks?
share|improve this question
1  
I too find it quite different to D7 but let me tell you that after 3 months of migrating a bug D7 app to D8 I am very happy with the possibilities D8 offers compared to D7. –  Ivan Jaros Mar 27 at 15:23

2 Answers 2

up vote 10 down vote accepted

Is this actually the case or am I misinterpreting whats happening.

Yep, a lot of D8 stuff is more verbose than that of its predecessor. A huge chunk of the codebase has moved to OO based on PSR-0, so you're always going to be writing more code, more files, creating more folders to put those files in, etc.

If this is the case then what is the advantage?

You can't directly apply an 'advantage' or 'disadvantage' to the concept of "D8 will use more code" (if that's indeed even true when you average it all out); it's simply an inevitability - the D8 page request has completely changed with the introduction of Symfony, so what was possible in a few lines in D7 just isn't possible with the same code now. The overall advantage is that Drupal gets all the power of Symfony driving it, and Symfony's framework/request lifecycle is generally considered to be stable and to use good practice. Plus, introducing a strict DI methodology to any kind of project is a big win in my opinion.

Would it be possible / desirable to use lots of wrapper functions to reduce the amount of code to achieve simple tasks?

Possible? Of course. Desirable? Depends on your opinion, and what you're creating a wrapper for. If it's something you do 10 times a day, and you can shorten 10 lines to 1 or 2 lines, then you'd be mad not to do it. Obviously that attitude transcends Drupal/PHP and should apply to any language/technology.

This is only a couple of examples but it seems that to achieve anything is D8 is going to take far more code.

The anything there isn't true - some things will be more verbose, some things less, it depends on exactly what you're looking to do.

share|improve this answer
    
Thanks - that's cleared things up a bit in my mind. I'm glad not everything will be more verbose. Sounds like it's going to be a steep learning curve - should keep me busy ;) –  Felix Eve Mar 26 at 12:17
2  
No probs - yeah, definitely get started now, there's a lot to learn! If I had to recommend some resources... 1. Potencier on DI (great de-mystification in case you're unfamiliar), and 2. the core change records (go back at least a year if you're just getting started with D8) –  Clive Mar 26 at 12:22
    
And/or take a look at backdropcms.org for yet another fork in the road ahead –  Jimajamma Mar 26 at 13:51
1  
Also what is gained is that core becomes less of an unmaintainable spaghetti mess allowing us to evolve faster. –  larowlan Mar 26 at 20:09

Also bear in mind that most of the code samples in change notices and such are lacking in context. That means they have to call out to context (like the DIC, or the request object, or whatever) explicitly, which is something that a well-behaved module should almost never do.

The biggest architectural shift in Drupal 8 is away from "there's always a globally-accessible function and some globally-accessible state data you can leverage" to "you need to tell the system to give you the context and services you're going to need". That's a prerequisite of testability and loose coupling, which are priorities for Drupal 8.

In places where you're expected to be doing certain things there are already utility methods. For instance, if you're using the common controller base class there is a $this->redirect($route_name) method ready for you; that's not appreciably more code than in Drupal 7, especially once you factor in the url() call.

That method is only available in controllers, of course... because if you're not in a controller you have no business sending a redirect. :-) You should be passing back data to the controller that has business meaning, and then the controller can interpret that as needing to send a redirect.

So it's not that it's "more code"; it's that the code needs to be better organized and separated. Once you do that, the code should actually be much more readable and far far easier to debug once you're not dealing with globals all the time.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.