Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete action not possible in read-only GridFieldDetailForm #11616

Open
2 tasks done
baukezwaan opened this issue Feb 17, 2025 · 1 comment
Open
2 tasks done

Delete action not possible in read-only GridFieldDetailForm #11616

baukezwaan opened this issue Feb 17, 2025 · 1 comment

Comments

@baukezwaan
Copy link

baukezwaan commented Feb 17, 2025

Module version(s) affected

5.3.0

Description

For gridfields with records that can only be viewed and deleted (not edited), there should be a delete button on the delete button on the GridFieldDetailForm. This has been fixed in the past, since the code specifically tries to solve this specific scenario. But the delete button is in a inactive state, so deleting is not possible for the user:

Image

How to reproduce

Reproduction by creating a Model with canEdit = false and canDelete = true
And a page where the read-only gridfield is shown.

ExampleModel.php

    class ExampleModel extends DataObject
    {

        private static $table_name = 'ExampleModel';

        private static $db = [
            'Title' => 'Varchar(75)',
            'Score' => 'Int'
        ];

        private static $has_one = [
            'Page' => Page::class
        ];

        public function canEdit($member = null)
        {
            return false;
        }

        public function canDelete($member = null)
        {
            return true;
        }
    }

Page.php

      class Page extends SiteTree
      {
          private static $db = [];

          public function getCMSFields()
          {
              $fields = parent::getCMSFields();

              $gridfield = GridField::create('Example', 'ExampleModels', $this->ExampleModels(), GridFieldConfig_RecordViewer::create());

              $fields->addFieldToTab('Root.Test', $gridfield);

              return $fields;
          }

      }
    

Possible Solution

In the method ItemEditForm() there is a solution made for this specific scenario:

see

if ($this->record->ID && (!$this->record->hasMethod('canEdit') || !$this->record->canEdit())) {
// Restrict editing of existing records
$form->makeReadonly();
// Hack to re-enable delete button if user can delete
if ($this->record->hasMethod('canDelete') && $this->record->canDelete()) {
$form->Actions()->fieldByName('action_doDelete')->setReadonly(false);
}

But in the view()-method, where this ItemEditForm() is called, the ->makeReadOnly() seems to be undoing the effort:

see

$form = $this->ItemEditForm();
$form->makeReadonly();

Validations

  • Check that there isn't already an issue that reports the same bug
  • Double check that your reproduction steps work in a fresh installation of silverstripe/installer (with any code examples you've provided)
@GuySartorelli
Copy link
Member

GuySartorelli commented Feb 19, 2025

Care will have to be taken when fixing this that sudo mode still doesn't allow performing a delete action if the form is protected.

It would probably also make sense for the delete action to be disabled by default for a regular readonly GridFieldDetailForm and have to be enabled either at a project level or a gridfield level.

@GuySartorelli GuySartorelli changed the title Delete action not possible in GridFieldDetailForm when using RecordViewer Delete action not possible in read-only GridFieldDetailForm Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants