2

I’ve read a few posts on this, and for the life of me I can’t get this working. I want to intercept a Channel Form submission, and modify the submitted data.

I’m using the ‘entry_submission_ready’ extension hook, which fires fine. I can access the $data array, but I cannot modify an element so that it is saved when the entry is saved.

I’m accessing the $data arary by reference ( &$data ), but that doesn’t make any difference.

Where an I going wrong?

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * ExpressionEngine - by EllisLab
 *
 * @package     ExpressionEngine
 * @author      ExpressionEngine Dev Team
 * @copyright   Copyright (c) 2003 - 2011, EllisLab, Inc.
 * @license     http://expressionengine.com/user_guide/license.html
 * @link        http://expressionengine.com
 * @since       Version 2.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Contact Notify Extension
 *
 * @package     ExpressionEngine
 * @subpackage  Addons
 * @category    Extension
 * @author      James Seavers
 * @link        http://www.symphonyonline.co.uk
 */

class Contact_notify_ext {

    public $settings        = array();
    public $description     = 'Notifies when an entry is created in contact request channel after changing some data';
    public $docs_url        = '';
    public $name            = 'Contact Notify';
    public $settings_exist  = 'n';
    public $version         = '1.0';

    private $EE;

    private $recipient = '[email protected]';

    /**
     * Constructor
     *
     * @param   mixed   Settings array or empty string if none exist.
     */
    public function __construct($settings = '')
    {
        $this->EE =& get_instance();
        $this->settings = $settings;
    }// ----------------------------------------------------------------------

    /**
     * Activate Extension
     *
     * This function enters the extension into the exp_extensions table
     *
     * @see http://codeigniter.com/user_guide/database/index.html for
     * more information on the db class.
     *
     * @return void
     */
    public function activate_extension()
    {
        // Setup custom settings in this array.
        $this->settings = array();

        $data = array(
            'class'     => __CLASS__,
            'method'    => 'change_data',
            'hook'      => 'entry_submission_ready',
            'settings'  => serialize($this->settings),
            'version'   => $this->version,
            'enabled'   => 'y'
        );

        $this->EE->db->insert('extensions', $data);         

    }   

    // ----------------------------------------------------------------------

    /**
     * change_data
     *
     * @param 
     * @return 
     */
    public function change_data($meta, &$data, $autosave=false)
    {
        $data['field_id_60'] = 'test';
    }

} 

Thanks in advance, James

1 Answer 1

2

Passing by reference doesn't work when calling extension hooks in EE.

If you want to alter the field data inside any of the API Channel Entries hooks, you need to change it directly in the API object, like so:

ee()->api_channel_entries->data['field_id_60'] = 'test';
3
  • Thanks Derek. I amended that, but it doesn't work when the data is submitted from the front-end via a Channel Form. Interestingly it does work when I then edit that entry in the Control Panel. Is there something else I have to add to get it to work with a front-end Channel Form?
    – seavers
    Mar 3, 2015 at 17:39
  • Channel Forms use a different hook - channel_form_submit_entry_start - and in that case you can simply work with $_POST. Mar 3, 2015 at 18:34
  • Spot on Dereck, thanks. Using the channel_form_submit_entry_start hook, I can modify the data like so: public function change_data($channel_form_obj) { $_POST['sc_job_title'] = 'testy'; }
    – seavers
    Mar 4, 2015 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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