Skip to content

Instantly share code, notes, and snippets.

@litzinger
Last active December 11, 2023 14:20
Show Gist options
  • Save litzinger/5f7f8f4abba565ff976b22868f95db99 to your computer and use it in GitHub Desktop.
Save litzinger/5f7f8f4abba565ff976b22868f95db99 to your computer and use it in GitHub Desktop.
Proposed changes to EE's Fluid Field - See https://github.com/ExpressionEngine/ExpressionEngine/issues/1643 for past convos
--- Model/FluidField_unedited.php 2023-07-17 11:25:23
+++ Model/FluidField.php 2023-07-07 16:49:14
@@ -119,7 +119,9 @@
$rows = ee()->extensions->call(
'fluid_field_get_field_data',
$this->field_id,
- $this->field_data_id
+ $this->field_data_id,
+ $this->fluid_field_id,
+ $this->entry_id
);
} else {
ee()->db->where('id', $this->field_data_id);
@@ -143,7 +145,8 @@
$field_data = ee()->extensions->call(
'fluid_field_get_all_data',
$field_data,
- $this->fluid_field_id
+ $this->fluid_field_id,
+ $this->entry_id
);
}
We have an array of fields but its not iterating them? This feels awkward. Changing it to the foreach seems to work fine with or without Publisher. I don't see negative side effects to making this change.
<?php
// $field_data is an array, regardless if it's a group of fields or single fields.
// Don't know why it's only accessing the first item in the array. This change works
// with the hook changes above, and without, and with single fields and grouped fields.
foreach ($fluid_field_data_groups as $field_data) {
$is_group = !is_null($field_data[0]->ChannelFieldGroup);
$view = ($is_group) ? 'fluid_field:fieldgroup' : 'fluid_field:field';
$viewData = [
'filters' => $filters,
'errors' => $this->errors,
'reorderable' => true,
'show_field_type' => false,
'field_filters' => $filter_options
];
if ($is_group) {
$field_group = $field_data[0]->ChannelFieldGroup; // might want to eager load this
$viewData = array_merge($viewData, [
'field_group' => $field_group,
'field_group_fields' => array_map(function ($field) use ($field_group) {
$f = $field->getField();
$f->setName($this->name() . '[fields][field_' . $field->getId() . '][field_group_id_' . $field_group->getId() . '][field_id_' . $f->getId() . ']');
return $f;
}, $field_data),
'field_name' => $field_group->short_name,
]);
$fields .= ee('View')->make($view)->render($viewData);
} else {
+ foreach ($field_data as $field_datum) {
+ $field = $field_datum->getField();
+ $field->setName($this->name() . '[fields][field_' . $field_datum->getId() . '][field_group_id_0][field_id_' . $field->getId() . ']');
+ $viewData = array_merge($viewData, [
+ 'field' => $field,
+ 'field_name' => $field_datum->ChannelField->field_name,
+ ]);
+ $fields .= ee('View')->make($view)->render($viewData);
+ }
- $field = $field_data[0]->getField();
- $field->setName($this->name() . '[fields][field_' . $field_data[0]->getId() . '][field_group_id_0][field_id_' . $field->getId() . ']');
- $viewData = array_merge($viewData, [
- 'field' => $field,
- 'field_name' => $field_data[0]->ChannelField->field_name,
- ]);
}
- $fields .= ee('View')->make($view)->render($viewData);
}
--- ft.fluid_field_unedited.php 2023-07-17 11:21:04
+++ ft.fluid_field.php 2023-07-15 07:12:15
@@ -387,7 +387,16 @@
$query->set($values);
$query->where('id', $fluid_field->field_data_id);
$query->update($fluid_field->ChannelField->getTableName());
+
+ if (ee()->extensions->active_hook('fluid_field_after_update_field') === true) {
+ ee()->extensions->call(
+ 'fluid_field_after_update_field',
+ $fluid_field,
+ $fluid_field->ChannelField->getTableName(),
+ $values
+ );
}
+ }
private function addField($order, $group, $field_id, array $values)
{
@@ -424,6 +433,15 @@
$fluid_field->field_data_id = $id;
$fluid_field->save();
+
+ if (ee()->extensions->active_hook('fluid_field_after_add_field') === true) {
+ ee()->extensions->call(
+ 'fluid_field_after_add_field',
+ $fluid_field,
+ $fluid_field->ChannelField->getTableName(),
+ $values,
+ $id
+ );
+ }
}
private function removeField($fluid_field)
@litzinger
Copy link
Author

Need to create diff, but recommended fluid_field_add_field signature change for consistency, line ~420 of ft.fluid_field.php

$values = ee()->extensions->call(
    'fluid_field_add_field',
    $fluid_field, // Add this
    $fluid_field->ChannelField->getTableName(),
    $values
);

@litzinger
Copy link
Author

litzinger commented Dec 11, 2023

Also need to move the fluid_field_get_all_data hook, it is in the wrong place.

litzinger/ExpressionEngine@614fdfb

@litzinger
Copy link
Author

litzinger commented Dec 11, 2023

I don't need this change for it to work with Publisher, just something I noticed along the way.

public function post_save($data)
    {
        // Prevent saving if save() was never called, happens in Channel Form
        // if the field is missing from the form
        if (($data = ee()->session->cache(__CLASS__, $this->name(), false)) === false) {
            return;
        }

        // I don't think the cache check above is working. __CLASS__ is always a blank string and it's not checking the array structure. 
        $cacheData = ee()->session->cache('Fluid_field_ft', $this->name(), false);

        if (empty($cacheData['fields'])) {
            return;
        }

@litzinger
Copy link
Author

In the ft.fluid_field.php file in the addField and updateField methods, between the two hooks wrap the db update calls in

if (!empty($values)) {

@litzinger
Copy link
Author

Can't change the param order ExpressionEngine/ExpressionEngine#3927 (comment)

Need to update fork to EE 7.4, and apply these changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment