Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Porting a D8 Module Under a Shifting API

Parent Feed: 

Six months ago or so it clearly stated in the Drupal docs to NOT create D8 modules due to the API not being finished. But people were still developing and porting D8 modules. I decided to try my hand at it and had a working module complete after a month or so. When I finally got Git access 6 months later to push my changes to Fancy Login, It had stopped working on the latest D8 build. This article covers the updates I had to make since the D8 API had changed.

UPDATE: this article is quite old and D8 core is now complete.

1. Moved FancyLoginSettingsForm.php rom folder fancy_login/src to fancy_login/src/Form folder

2. In FancyLoginSettingsForm.php file changed translate functions t(). for example

t('Text !url.', array('!url' => 'background-

color')) to t('Text background-color', array('@url' =>

'http://themeshift.com'))

3. Moved the function user_login_block_ajax_callback from file FancyLoginSettingsForm.php to

fancy_login.module file.

4. Moved libraries such as

Drupal\Core\Ajax\AjaxResponse;

Drupal\lightbox_login\Ajax\lightboxLoginRefreshPageCommand;

Drupal\lightbox_login\Ajax\lightboxLoginRedirectCommand;

from FancyLogin_loginForm.php file to fancy_login.module file.

5. Deleted library

use Drupal\Core\Ajax\AppendCommand;

from FancyLogin_loginForm.php file.

6. Moved function ser_pass_ajax_callback from FancyLogin_passForm.php file to fancy_login.module

file.

7. Moved library

use Drupal\lightbox_login\Ajax\lightboxLoginClosePopupCommand;

from FancyLogin_passForm.php file to fancy_login.module file.

8. In folder fancy_login/src/Plugin/Block

a) Added rows

/**

* @file

* Contains \Drupal\fancy_login\Plugin\Block\FancyLoginBlock.

*/

to FancyLoginBlock.php file.

b) Changed code from

$GLOBALS['user']->isAnonymous() || !empty($GLOBALS['menu_admin'])

to

\Drupal::currentUser()->isAnonymous()

in build function in FancyLoginBlock.php file.

9. In folder fancy_login/src/Controller

a) Deleted function

drupal_process_attached($form) in ajax_callback function in FancyLoginController.php file.

b) Changed row

$scripts = _drupal_add_js();

to

$scripts = drupal_js_defaults();

in ajax_callback function in FancyLoginController.php file.

10. In fancy_login.module file

a) in functions user_login_block_ajax_callback and user_pass_ajax_callback

changed argument to no longer pass by reference from

array &$form

to

$form

b) In functions user_login_block_ajax_callback and user_pass_ajax_callback

changed rows

$messages = array('#theme' => 'status_messages');

$messages = drupal_render($messages);

to

$renderer = \Drupal::service('renderer');

$messages = array('#type' => 'status_messages');

$messages = $renderer->renderRoot($messages);

c) In function user_login_block_ajax_callback

changed row

$response->addCommand(new AppendCommand('#lightbox_login_user_login_block_wrapper',

$messages));

to

$selector = '#fancy_login_user_login_block_wrapper';

...

$response->addCommand(new Ajax\AppendCommand($selector, $messages));

d) in function user_pass_ajax_callback

changed row

$response->addCommand(new AppendCommand('#lightbox_login_user_pass_block_wrapper',

$messages));

to

$selector = '#lightbox_login_user_pass_block_wrapper';

...

$response->addCommand(new Ajax\AppendCommand($selector, $messages));

e) in function fancy_login_page_bottom

changed row

GLOBALS['user']->isAnonymous() || !empty($GLOBALS['menu_admin'])

to

\Drupal::currentUser()->isAnonymous()

f) in function fancy_login_page_bottom

changed row

$login_path = \Drupal::service('path.alias_manager')->getAliasByPath('user/login');

to

$login_path = \Drupal::service('path.alias_manager')->getAliasByPath('/user/login');

g) in function fancy_login_page_bottom

changed row

'callback' =>

'Drupal\lightbox_login\Form\lightboxLogin_loginForm::user_login_block_ajax_callback',

to

'callback' => 'user_login_block_ajax_callback',

h) in function fancy_login_page_bottom

changed row

'callback' => 'Drupal\lightbox_login\Form\lightboxLogin_passForm::user_pass_ajax_callback',

to

'callback' => 'user_pass_ajax_callback',

11. In folder fancy_login/js in file fancy_login.js

changed all methods to use each in JQuery

$("body").once("fancy-login-init", function () {

to

$("body").once("fancy-login-init").each(function () {

All of these changes were necessary for the module to work.

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web