We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

expressionengine safecracker hook

Development and Programming

alex00's avatar
alex00
35 posts
about 14 years ago
alex00's avatar alex00

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;
  }
 }
}
       
Dan Decker's avatar
Dan Decker
7,338 posts
about 14 years ago
Dan Decker's avatar Dan Decker

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!

       
Bhashkar Yadav's avatar
Bhashkar Yadav
727 posts
about 14 years ago
Bhashkar Yadav's avatar Bhashkar Yadav

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 like

public function clean($sc)
{
 $sc->entry['title'] = 'test';
}
       
alex00's avatar
alex00
35 posts
about 14 years ago
alex00's avatar alex00
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 like
public 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.

       
alex00's avatar
alex00
35 posts
about 14 years ago
alex00's avatar alex00
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!

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’.

       
kenyabob's avatar
kenyabob
2 posts
13 years ago
kenyabob's avatar kenyabob

Did you ever figure out if you can save a new title at the end of the submission?

       
the3mus1can's avatar
the3mus1can
426 posts
13 years ago
the3mus1can's avatar the3mus1can

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’].

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.