records = $records; $this->parentType = $parentType; $this->parentMethod = $parentMethod; $this->childMethod = $childMethod; } /** * {@inheritdoc} */ public function augmentColumns($gridField, &$columns) { if (!in_array('MergeAction', $columns)) { $columns[] = 'MergeAction'; } return $columns; } /** * {@inheritdoc} */ public function getColumnsHandled($gridField) { return ['MergeAction']; } /** * {@inheritdoc} */ public function getColumnContent($gridField, $record, $columnName) { if ($columnName === 'MergeAction' && $record->{$this->childMethod}()->Count() > 0) { $dropdown = DropdownField::create('Target', 'Target', $this->records->exclude('ID', $record->ID)->map()); $dropdown->setAttribute('id', 'Target_'.$record->ID); $prefix = strtolower($this->parentMethod . '-' . $this->childMethod); $action = GridFieldFormAction::create( $gridField, 'MergeAction' . $record->ID, 'Move', 'merge', [ 'record' => $record->ID, 'target' => $prefix . '-target-record-' . $record->ID, ] ); $action->setExtraAttributes([ 'data-target' => $prefix . '-target-record-' . $record->ID ]); $action->addExtraClass('btn btn-primary btn-sm blog-merge-action'); return $dropdown->Field() . $action->Field() . 'Move posts to'; } return null; } /** * {@inheritdoc} */ public function getColumnAttributes($gridField, $record, $columnName) { return ['class' => 'MergeAction']; } /** * {@inheritdoc} */ public function getColumnMetadata($gridField, $columnName) { return ['title' => 'Move posts to']; } /** * {@inheritdoc} */ public function getActions($gridField) { return ['merge']; } /** * {@inheritdoc} */ public function handleAction(GridField $gridField, $actionName, $arguments, $data) { if ($actionName === 'merge') { $controller = Controller::curr(); $request = $controller->getRequest(); $target = $request->requestVar($arguments["target"]); $parentType = $this->parentType; $fromParent = $parentType::get()->byId($arguments['record']); $toParent = $parentType::get()->byId($target); $posts = $fromParent->{$this->childMethod}(); foreach ($posts as $post) { $relationship = $post->{$this->parentMethod}(); $relationship->remove($fromParent); $relationship->add($toParent); } } } }