Search code examples
drupal-7swiper.js

How to use hook_swiper_options_alter($node, $plugin_options) {} in Drupal-7


I have been searching for a few days to figure out how to change the options for the swiper (v 7.x-1.4) module in Drupal-7. The documentation is clear-as-mud explaining how the module expects this hook to be used. I'm looking for a simple code example on how to implement the following options from the swiper API:

autoplay
prevButton
nextButton
autoplayDisableOnInteraction

The only documentation reference I have been able to find is from the README.txt in the module:

...
You can also add, change and remove, any of API options of the Swipers, 
just you need to implement a hook:
hook_swiper_options_alter($node, $plugin_options) {}

This way the module will handle pass these options to the script that 
instantiates the swiper Plugin.
...

I'm fairly new to Drupal, but I am trying to learn. I have attempted to create a simple custom module to implement these options. I have called my module myCustom, created the /drupal/sites/all/modules/myCustom directory with the following files:

myCustom.info:

name = myCustom
description = customize swiper
package = me
version = 0.02
core = 7.x

files[] = myCustom.module

myCustom.module:

<?php
function myCustom_swiper_options_alter($node, $plugin_options) 
{
  $plugin_options += (
    nextButton: '.swiper-button-next',
    prevButton: '.swiper-button-prev',
    paginationClickable: true,
    autoplay: 2500,
    autoplayDisableOnInteraction: true
  );
  return($node, $plugin_options);
}

I know I have multiple problems. Drupal refuses to enable my module as-is and I can not figure out why. I have checked the admin->reports->recent log messages report and found nothing relevant to at least help me troubleshoot.

Any ideas how I can fix this? Does anyone have a working example of code that I can copy and modify to get this hook working?

Thank you in advance for any assistance!


Solution

  • You may want to read through this documentation: Writing module .info files (Drupal 7.x).

    • Remove this line from your .info file: files[] = myCustom.module. Drupal will automatically read through the .module file.

    • As you defined a version in your .info file this may need your attention: Release naming conventions, but actually you can just leave that out as well, it's not mandatory.

    • Since you're using a hook from that swiper module, I recommend to set it as a dependency in your custom module's .info file as: dependencies[] = swiper to prevent unmet dependency errors.

    • Change the $plugin_options array to a php array & do not return anything:

      <?php
      
      function YOUR_MODULE_swiper_options_alter($node, &$plugin_options) {
      
          $plugin_options += array( 
              'nextButton' => '.swiper-button-next',
              'prevButton' => '.swiper-button-prev',
              'paginationClickable' => true,
              'autoplay' => 2500,
              'autoplayDisableOnInteraction' => true,
          );
      
      }
      
    • Additionally: Try to refrain from using capitals in module names as per machine name (module dir name). If you take a look at other Drupal modules in /modules or sites/all/modules they're all lowercased. (You can leave the name in your .info file which also represents your module in the modules list as you have now.)