From 4fbf6612e74e5c71050971e099e1a07e25cd1888 Mon Sep 17 00:00:00 2001 From: Andy Hebrank Date: Fri, 5 Oct 2018 15:25:39 -0400 Subject: [PATCH] add relationships support for grids --- .../third_party/json/pi.json.php | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/system/expressionengine/third_party/json/pi.json.php b/system/expressionengine/third_party/json/pi.json.php index dbae472..fe2b205 100644 --- a/system/expressionengine/third_party/json/pi.json.php +++ b/system/expressionengine/third_party/json/pi.json.php @@ -35,6 +35,7 @@ class Json protected $entries_grid_cols; protected $entries_rel_data; protected $entries_relationship_data; + protected $entries_grid_relationship_data; protected $entries_playa_data; protected $entries_channel_files_data; protected $image_manipulations = array(); @@ -431,7 +432,12 @@ protected function entries_grid($entry_id, $field, $field_data) foreach ($this->entries_grid_cols[$field['field_id']] as $col_id => $col) { - $row[$col['col_name']] = $grid_row['col_id_'.$col_id]; + $val = $grid_row['col_id_' . $col_id]; + if ($col['col_type'] == 'relationship') + { + $val = $this->entries_grid_relationship($col_id, $row['row_id'], $entry_id); + } + $row[$col['col_name']] = $val; } $data[] = $row; @@ -502,6 +508,42 @@ protected function entries_relationship($entry_id, $field, $field_data) return array(); } + protected function entries_grid_relationship($grid_col_id, $grid_row_id, $entry_id) + { + if (is_null($this->entries_grid_relationship_data)) + { + $query = ee()->db->select('parent_id, child_id, grid_field_id, grid_col_id, grid_row_id') + ->where_in('parent_id', $this->entries_entry_ids) + ->where('grid_col_id', $grid_col_id) + ->order_by('order', 'asc') + ->get('relationships'); + + foreach ($query->result_array() as $row) + { + if ( ! isset($this->entries_grid_relationship_data[$grid_col_id][$row['parent_id']][$row['grid_row_id']])) + { + $this->entries_grid_relationship_data[$grid_col_id][$row['parent_id']][$row['grid_row_id']] = array(); + } + + if ( ! isset($this->entries_grid_relationship_data[$grid_col_id][$row['parent_id']][$row['grid_row_id']])) + { + $this->entries_grid_relationship_data[$grid_col_id][$row['parent_id']][$row['grid_row_id']] = array(); + } + + $this->entries_grid_relationship_data[$grid_col_id][$row['parent_id']][$row['grid_row_id']][] = (int) $row['child_id']; + } + + $query->free_result(); + } + + if (isset($this->entries_grid_relationship_data[$grid_col_id][$entry_id][$grid_row_id])) + { + return $this->entries_grid_relationship_data[$grid_col_id][$entry_id][$grid_row_id]; + } + + return array(); + } + protected function entries_playa($entry_id, $field, $field_data) { if (is_null($this->entries_playa_data)) @@ -1058,6 +1100,7 @@ protected function initialize($which = NULL) $this->entries_matrix_rows = NULL; $this->entries_rel_data = NULL; $this->entries_relationship_data = NULL; + $this->entries_grid_relationship_data = NULL; $this->entries_playa_data = NULL; $this->entries_channel_files_data = NULL; break;