Remove "Unlink" option from Gridfields

Silverstripe Version: 4.5

Question: Remove “Unlink” option from Gridfields

Having trouble removing the “Unlink” option from a has_many field in the grid field.
silverstripeehey

use SilverStripe\Versioned\GridFieldUnlinkAction;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;

[...]

public function getCMSFields()
  {
    $dataMapsGrid = GridField::create('DataMaps', 'Data Maps', $this->DataMaps(), GridFieldConfig_RelationEditor::create());

    $dataMapsGrid->getConfig()
    // Was hoping this would remove the "Unlink" action
    ->removeComponentsByType(GridFieldUnlinkAction::class)
    // Removes "Link Existing" action
    ->removeComponentsByType(GridFieldAddExistingAutocompleter::class);

    $fields = new FieldList(
      new TextField('Title', 'Title', null, 128),
      $dataMapsGrid
    );
    $this->extend('updateCMSFields', $fields);
    return $fields;
  }

By the look of it, you might be better off using a different configuration to start with. Instead of the GridFieldConfig_RelationEditor, you might try a GridFieldConfig_RecordEditor instead.

1 Like