0

I'm a Drupal procedural programmer new to OOP and enjoying it, so some of the techniques are new to me.

I'm getting a fatal error when I'm calling a standard drupal function from within code called within an object that creates a custom Rule. It appears that it's looking for the function drupal_json_encode_helper on the Action object rather than from where it's supposed to look. But why? And how to I tell it to search within Drupal-land rather than object-land?

I'm on D7 with xautoload and composer, modifying the fluxdropbox module that uses fluxservices to use PSR-0 for external libraries.

"Fatal error: Call to undefined function Drupal\fluxdropbox\Plugin\Rules\Action\drupal_json_encode_helper()"

Here's the code:

<?php

/**
 * @file
 * Contains MakeNodes.
 */

namespace Drupal\fluxdropbox\Plugin\Rules\Action;

use Drupal\fluxdropbox\Plugin\Service\DropboxAccountInterface;
use Drupal\fluxdropbox\Rules\RulesPluginHandlerBase;

/**
 * Action for posting a status message on a page.
 */
class MakeNodes extends RulesPluginHandlerBase implements \RulesActionHandlerInterface {

  /**
   * Defines the action.
   */
  public static function getInfo() {
    return static::getInfoDefaults() + array(
      'name' => 'fluxdropbox_make_nodes',
      'label' => t('Make Nodes from Text Fiels'),
      'parameter' => array(
        'account' => static::getAccountParameterInfo(),
      ),
    );
  }

  /**
   * Executes the action.
   */
  public function execute(DropboxAccountInterface $account) {
            //here I build a nice array
            file_put_contents(drupal_json_encode_helper($feed_array), $path . '/novel.txt');

  }
}
3
  • Is this for D7 or D8? Any reason not to use the standard Rules hooks? e.g hook_rules_action_info etc.. May 25 '14 at 23:05
  • It's D7 code that uses xautoload. I'm copying another module's code. Everything works and it would be a pain to re-write it, I'm just hitting this one error related to autoloading. May 26 '14 at 0:39
  • Just saying, xautoload (and autoload in general) does not help you with procedural code.
    – donquixote
    Jun 13 '14 at 10:57
1

It happens because drupal_json_encode() is defined in includes/json-encode.inc, which is not a file that is normally included when Drupal boots. To use that function, you should use code similar to the one drupal_json_encode() uses, although you really should use drupal_json_encode().

include_once DRUPAL_ROOT . '/includes/json-encode.inc';
file_put_contents(drupal_json_encode_helper($feed_array), $path . '/novel.txt');

The code I would use is the following one.

file_put_contents(drupal_json_encode($feed_array), $path . '/novel.txt');
1
  • Yep, this was it. Figured this out, actually. The objective-ness of the error had me confused. May 26 '14 at 21:47
0

OK, if you change your code from

file_put_contents(drupal_json_encode_helper($feed_array), $path . '/novel.txt');

to

file_put_contents(drupal_json_encode($feed_array), $path . '/novel.txt');

The correct file will be included for json encoding.

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.