1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-13 10:15:28 +02:00

Add new Fieldtype::saveFieldReady(Field $field) hook that is called right before a Field object is about to be saved. For newly created fields, the given $field will have $field->id==0

This commit is contained in:
Ryan Cramer
2023-02-09 09:54:28 -05:00
parent e86eb7fcf8
commit 1171241f5d
2 changed files with 27 additions and 5 deletions

View File

@@ -5,7 +5,7 @@
* *
* Manages collection of ALL Field instances, not specific to any particular Fieldgroup * Manages collection of ALL Field instances, not specific to any particular Fieldgroup
* *
* ProcessWire 3.x, Copyright 2022 by Ryan Cramer * ProcessWire 3.x, Copyright 2023 by Ryan Cramer
* https://processwire.com * https://processwire.com
* *
* #pw-summary Manages all custom fields in ProcessWire, independently of any Fieldgroup. * #pw-summary Manages all custom fields in ProcessWire, independently of any Fieldgroup.
@@ -357,6 +357,7 @@ class Fields extends WireSaveableItems {
} }
if(!$item->type) throw new WireException("Can't save a Field that doesn't have it's 'type' property set to a Fieldtype"); if(!$item->type) throw new WireException("Can't save a Field that doesn't have it's 'type' property set to a Fieldtype");
$item->type->saveFieldReady($item);
if(!parent::___save($item)) return false; if(!parent::___save($item)) return false;
if($isNew) $item->type->createField($item); if($isNew) $item->type->createField($item);
@@ -773,7 +774,7 @@ class Fields extends WireSaveableItems {
// so use verbose/slow method to delete the field from pages // so use verbose/slow method to delete the field from pages
$ids = $this->getNumPages($field, array('template' => $template, 'getPageIDs' => true)); $ids = $this->getNumPages($field, array('template' => $template, 'getPageIDs' => true));
$items = $this->wire('pages')->getById($ids, $template); $items = $this->wire()->pages->getById($ids, $template);
foreach($items as $page) { foreach($items as $page) {
try { try {
@@ -791,7 +792,7 @@ class Fields extends WireSaveableItems {
// large number of pages to operate on: use fast method // large number of pages to operate on: use fast method
$database = $this->wire('database'); $database = $this->wire()->database;
$table = $database->escapeTable($field->getTable()); $table = $database->escapeTable($field->getTable());
$sql = "DELETE $table FROM $table " . $sql = "DELETE $table FROM $table " .
"INNER JOIN pages ON pages.id=$table.pages_id " . "INNER JOIN pages ON pages.id=$table.pages_id " .

View File

@@ -47,6 +47,7 @@
* @method Field cloneField(Field $field) * @method Field cloneField(Field $field)
* @method void renamedField(Field $field, $prevName) * @method void renamedField(Field $field, $prevName)
* @method void savedField(Field $field) * @method void savedField(Field $field)
* @method void saveFieldReady(Field $field)
* @method void install() * @method void install()
* @method void uninstall() * @method void uninstall()
* *
@@ -992,8 +993,9 @@ abstract class Fieldtype extends WireData implements Module {
if($a == 'CHARSET') $info['charset'] = $b; if($a == 'CHARSET') $info['charset'] = $b;
} }
} }
if(!$info['engine']) $info['engine'] = $this->wire('config')->dbEngine; $config = $this->wire()->config;
if(!$info['charset']) $info['charset'] = $this->wire('config')->dbCharset; if(!$info['engine']) $info['engine'] = $config->dbEngine;
if(!$info['charset']) $info['charset'] = $config->dbCharset;
if($info['engine']) $info['engine'] = str_replace(array('MYISAM', 'INNODB'), array('MyISAM', 'InnoDB'), $info['engine']); if($info['engine']) $info['engine'] = str_replace(array('MYISAM', 'INNODB'), array('MyISAM', 'InnoDB'), $info['engine']);
$info['transactions'] = $info['engine'] == 'InnoDB'; $info['transactions'] = $info['engine'] == 'InnoDB';
} }
@@ -1505,6 +1507,7 @@ abstract class Fieldtype extends WireData implements Module {
* Most Fieldtypes don't need to do anything here, but this exists just in case. * Most Fieldtypes don't need to do anything here, but this exists just in case.
* *
* #pw-internal * #pw-internal
* #pw-hooker
* *
* @param Field $field * @param Field $field
* @param string $prevName Previous name (current name can be found in $field->name) * @param string $prevName Previous name (current name can be found in $field->name)
@@ -1513,12 +1516,30 @@ abstract class Fieldtype extends WireData implements Module {
public function ___renamedField(Field $field, $prevName) { public function ___renamedField(Field $field, $prevName) {
} }
/**
* Hook called by Fields::save() when a field is about to be saved
*
* If field is a new field it will not yet have an id.
*
* #pw-internal
* #pw-hooker
*
* @param Field $field
* @since 3.0.212
*
*/
public function ___saveFieldReady(Field $field) {
}
/** /**
* Called when Field using this Fieldtype has been saved * Called when Field using this Fieldtype has been saved
* *
* This is primarily so that Fieldtype modules can identify when their fields are * This is primarily so that Fieldtype modules can identify when their fields are
* saved without having to add a hook to the $fields API var. * saved without having to add a hook to the $fields API var.
* *
* #pw-internal
* #pw-hooker
*
* @param Field $field * @param Field $field
* @since 3.0.171 * @since 3.0.171
* *