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

before_channel_entry_save hook alter form data

How Do I?

Andrés Molina's avatar
Andrés Molina
43 posts
5 years ago
Andrés Molina's avatar Andrés Molina

im have a custom Extension, works fine, but dont change the data

My Code

public function PopulateData($entry,$values)
 {
    
   
   
  $entry->setProperty("my_custom_field_name", 'This is My Custom Field Content');
  $values["my_custom_field_name"]='This is My Custom Field Content';
   
   
 
  
 }

Entry Is Saved, without Changes

       
Andrés Molina's avatar
Andrés Molina
43 posts
5 years ago
Andrés Molina's avatar Andrés Molina

OK, real world Working Example in 2019

<?php
 

 
 
class Tcdcl_cart_ext
{
 public function __construct()
 {
  $this->version = ee('Addon')->get('tcdcl_cart')->getVersion();
 }

 /**
  * Activate extension, need this to register Hook, you can unninstall and install and hoock is activated and working
  */
 public function activate_extension()
 {
  $hooks = array(
   'before_channel_entry_save' => 'PopulateData',
   
   
   
  );
  

  foreach ($hooks as $hook => $method)
  {
   ee('Model')->make('Extension', [
    'class'    => __CLASS__,
    'method'   => $method,
    'hook'     => $hook,
    'settings' => [],
    'version'  => $this->version,
    'enabled'  => 'y'
   ])->save();
  }
 }

  
 public function PopulateData($entry,$values)
 {
   
   
  
  $entry->set(array("title"=>"Entry Custom Title, replace Form Filled title"));  
   
   
 
  
 }
 
  
 
  
  

 /**
  * Disable extension
  */
 function disable_extension()
 {
  ee('Model')->get('Extension')
   ->filter('class', __CLASS__)
   ->delete();
 }

 /**
  * Update extension
  */
 function update_extension($current = '')
 {
  if ($current == '' OR $current == $this->version)
  {
   return FALSE;
  }
 }
}

// EOF
       
Brian Litzinger's avatar
Brian Litzinger
693 posts
5 years ago
Brian Litzinger's avatar Brian Litzinger

Those events don’t return anything, or let you modify the object. If you want to make changes to it, you have to call save()

$entry
    ->set(array("title"=>"Entry Custom Title, replace Form Filled title"))
    ->save();

But be careful b/c that may just end up calling before_channel_entry_save again and creating a loop.

       
Andrés Molina's avatar
Andrés Molina
43 posts
5 years ago
Andrés Molina's avatar Andrés Molina

Thanks, for now im try writing custom Grid on FrontEnd(only Show Cart data, and hidden GRid Fields), and then compare if the form data, are the same in the “Cart” data.

Im have a Custom Cart, with Product ID, price, Quantity, total Price for each row, Channel “Quotation” have a Grid This grid Have related Field to Products Channel and integer fields -> price, Quantity, total Price

       

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.