Our Blog

Porting a D8 Module Under a Shifting API

Six months ago or so it clearly stated in the Drupal docs to NOT create D8 modules do to the API not being finished. But still right and left there were people developing and porting D8 modules, clients were asking for it as experience and there was a log of hype. I decided to try my hand at it and had a working module as of 6 months ago. 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 latest changes to the API made during this period and the changes I had to make to do last minute API changes.
module Fancy Login.

1. moved file FancyLoginSettingsForm.php from folder fancy_login/src to fancy_login/src/Form folder

2. in FancyLoginSettingsForm.php file changed translate functions 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 to the module began to work.

Hope after we got this official D 8 release – we will not get such global changes in future.

Thanks.

Comments ( 0 )

    Leave A Comment

    Your email address will not be published. Required fields are marked *