1

I am writing PHP code in a rule, where I include another PHP file.

include_once 'google-api-php-client/src/Google/autoload.php';

Where do I need to include files?

0

Try the standard PHP way:

  1. require 'google-api-php-client/src/Google/autoload.php';
  2. include_once 'includes/google-api-php-client/src/Google/autoload.php';
  3. include ('includes/google-api-php-client/src/Google/autoload.php');

or Drupal way:

include_once DRUPAL_ROOT . '/includes/google-api-php-client/src/Google/autoload.php';
  • module_load_include(); function - [Drupal API documentation][1] - to include any PHP files from within your or other modules where needed, [1]: api.drupal.org/api/drupal/includes%21module.inc/function/… – parth.k Jun 9 '16 at 9:36
  • seems this is not working for me. even i didnt any kind of error – Ankita Kashyap Jun 9 '16 at 10:21
  • Also where i need to place google-api-php-client folder? – Ankita Kashyap Jun 9 '16 at 10:23
  • Wherever you want to @AnkitaKashyap; a custom module or in the libraries folder might make most sense, but it depends what you're doing – Clive Jun 9 '16 at 10:31
  • @Clive: i am new to drupal. i place external directory in sites/all/libraries folder .. and in rules i am not getting any error – Ankita Kashyap Jun 9 '16 at 10:35
3
  1. You can use module_load_include() to include a PHP file.

    module_load_include('php', $type, 'includes/google-api-php-client/src/Google/autoload');
    

    Where type can be either theme name or module name. I guess in this you are building some drupal module so it must be module type. In which case it will look for autoload.php file inside your name module directory. However internally it will do the require_once instead of include_once :)

  2. You can use Libraries API module to include a php file. For this you will have to keep this google-api-php-client inside libraries folder. And you will perform these steps to implement it.

    function hook_libraries_info() {
      $path = libraries_get_path('google-api-php-client');
      set_include_path($path."/src/" . PATH_SEPARATOR .  get_include_path());
      $libraries['google-api-php-client'] = array(
      'name' => 'Google OAuth2 Library',
      'vendor url' => 'http://code.google.com/p/google-api-php-client',
      'download url' => 'http://github.com/google/google-api-php-client/archive/master.zip',
      'version arguments' => array(
        'file' => 'src/Google/Client.php',
        'pattern' => '/LIBVER = "([0-9\.a-z]+)/',
        'lines' => 40,
      ),
      'versions' => array(
        '1.0.5-beta' => array(
          'files' => array(
            'php' => array(
              'src/Google/Client.php',
            )
          ),
        ),
        '1.1.5' => array(
          'files' => array(
            'php' => array(
              'src/Google/autoload.php',
            )
          )
        )
      ),
      'files' => array(
        'php' => array(
          'src/Google/Client.php',
        ),
       ),
      );
      return $libraries;
    }
    

    Note: The version is 1.0.5-beta, so you may need to change the version as per your API version.

    Then to include this library in another module.

    function hook_init() {
      $path = libraries_get_path('google-api-php-client');
      if ($path) {
        set_include_path($path."/src/" . PATH_SEPARATOR . get_include_path());
      }
    }
    
  3. Since it comes it composer.json/autoload.php you can use these modules to load it automatically from libraries folder. X Autoload, Composer Autoload, Composer Vender Autoload, although you will still need to define the location inside your module.

     

    Using the XAutoload module

       function mymodule_libraries_info() {
         return array(
           'google-api-php-client' => array(
             'name' => 'Google Client Library',
             'xautoload' => function($adapter) {
               /** @var \Drupal\xautoload\Adapter\LocalDirectoryAdapter $adapter */
               // Scan sites/all/libraries/google-api-php-client/composer.json to look for
               // autoload information.
               $adapter->composerJson('composer.json');
             }
           )
         );
       }
    
  • where i need to place google-api-php-client folder? – Ankita Kashyap Jun 9 '16 at 10:23
  • For 2 and 3. place it inside sites/all/libraries folder. – sarathkm Jun 9 '16 at 10:27
  • Can't be do in simple way? – Ankita Kashyap Jun 9 '16 at 10:37
  • Well the most simple way is to place it inside a module. Say gapi and place the google-api-php-client inside it somewhere say includes within gapi module. Now you can get the autoload.php function from any other module or php or inc or from gapi.module itself file by just saying module_load_include('php', 'gapi', '/includes/google-api-php-client'); at the top of file or inside functions where you want to get those functions. – sarathkm Jun 9 '16 at 10:47
  • i am not getting any error.under debug mode i am able to see the condition php_eval evaluated to FALSE – Ankita Kashyap Jun 9 '16 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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