I’m trying to modify the entry title upon safecracker submission. I’m new to hooks and still experimenting. For the sake of simplifying things, when an entry is submitted via safecracker form, i want to modify $_POST[‘title’] to ‘test’. I created the following extension to do this and i’m using the hook safecracker_submit_entry_end. In the clean() function i have $_POST[‘title’] = ‘test’;. but that is not working. title is not being replaced with with the word test. What am i doing wrong.
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Clean_ext {
public $settings = array();
public $description = 'clean title';
public $docs_url = 'http://whatever.com';
public $name = 'clean';
public $settings_exist = 'n';
public $version = '1.0';
private $EE;
public function __construct($settings = '')
{
$this->EE =& get_instance();
$this->settings = $settings;
}
public function activate_extension()
{
$this->settings = array();
$data = array(
'class' => __CLASS__,
'method' => 'clean',
'hook' => 'safecracker_submit_entry_end',
'settings' => serialize($this->settings),
'version' => $this->version,
'enabled' => 'y'
);
$this->EE->db->insert('extensions', $data);
}
public function clean($sc)
{
$_POST['title'] = 'test';
}
function disable_extension()
{
$this->EE->db->where('class', __CLASS__);
$this->EE->db->delete('extensions');
}
function update_extension($current = '')
{
if ($current == '' OR $current == $this->version)
{
return FALSE;
}
}
}Hi alex00,
We don’t offer support for add-on development, but I can suggest that you use the ExpressionEngine input class rather than the $_POST array.
$this->EE->input->post('title');Have you done any var_dump() to see what is being passed to the extension? It’s possible that by the time the hook is called, the entry has been turned into an object instead of being in the $_POST array.
I’m moving this over into the proper Development and Programming forum so the talented Community can help you get the answers you need.
Cheers!
safecracker_submit_entry_end hook works while an entry is being saved. So you should use safecracker_submit_entry_start hook: and update “clean” function likepublic function clean($sc) { $sc->entry['title'] = 'test'; }
I tried your suggestion $sc->entry[‘title’] = ‘test’; using safecracker_submit_entry_start but it’s not having any effect on title. Title is not being saved as ‘test’. I also tried safecracker_submit_entry_end but no success. What do you suggest could be the issue.
Hi alex00, We don’t offer support for add-on development, but I can suggest that you use the ExpressionEngine input class rather than the $_POST array.Have you done any var_dump() to see what is being passed to the extension? It’s possible that by the time the hook is called, the entry has been turned into an object instead of being in the $_POST array. I’m moving this over into the proper Development and Programming forum so the talented Community can help you get the answers you need. Cheers!$this->EE->input->post('title');
Your suggestion $this->EE->input->post(‘title’) = ‘test’; doesn’t work and gives an error Can’t use method return value in write context
I don’t believe we can use $this->EE->input->post(‘title’) to overwrite over the title field. i don’t know what you had in mind when you suggested the use of $this->EE->input->post(‘title’). All i’m trying to do is overwrite the the title post variable $_POST[‘title’] = ‘test’.
You can not use the safecracker_submit_entry_end to modify the entry data before it gets saved. You need to use safecracker_submit_entry_start. The safecracker_submit_entry_end gets called after all saving and updating of entry data is done.
Also you cannot assign a value to a method, like this $this->EE->input->post(‘title’) = ‘test’, you will need to do this $_POST[‘title’] = ‘test’; You should use $_POST and not $sc->entry[‘title’].
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.