GithubHelp home page GithubHelp logo

Comments (6)

tractorcow avatar tractorcow commented on July 18, 2024

The problem with swapping this order is that you then have the opposite problem; user code that extends getCMSFields is then no longer able to update fields added by extensions.

The real question is, does the convention need to change?

I would argue not, as the above solution can be solved by user code that determines when fields should be overridden or not.

See the blog module for an example of how this is done:

    function getCMSFields() {

        SiteTree::disableCMSFieldsExtensions();
        $fields = parent::getCMSFields();
        SiteTree::enableCMSFieldsExtensions();

        // do stuff

        $this->extend('updateCMSFields', $fields);

        return $fields;
    }

I think a better question is, can we make it simpler and easier for users to do the above? (rather than changing the convention).

Is this better?

SiteTree.php

    /**
     * Delays extension of getCMSFields
     * @param callable $callback
     * @return FieldList
     */
    protected function beforeExtendingCMSFields($callback) {
        if(!self::$runCMSFieldsExtensions) {
            user_error('Calls to SiteTree::beforeExtendingCMSFields cannot be nested', E_USER_ERROR);
        }
        self::disableCMSFieldsExtensions();
        $fields = call_user_func(array($this, "parent::getCMSFields"));
        $callback($fields);
        self::enableCMSFieldsExtensions();
        $this->extend('updateCMSFields', $fields);
        return $fields;
    }

MyPage.php

    public function getCMSFields() {
        $fields = $this->beforeExtendingCMSFields(function(FieldList &$fields){
            $fields->addFieldToTab('Root.Main', new TextField('Test', 'Test'), 'Content');
        });

        // after extending updateCMSFields
        $fields->removeByName('Root.GoogleSitemaps');

        return $fields;
    }

from silverstripe-framework.

wilr avatar wilr commented on July 18, 2024

Like your thinking! That defiantly makes the code a bit more explicit as to what it's trying to achieve rather than the disable() calls.

from silverstripe-framework.

tractorcow avatar tractorcow commented on July 18, 2024

Shame the code above doesn't work very well, but I'll rough something up and do some better testing.

The problem is that there's no object context in a closure, so calls to parent::getCMSFields is impossible, and workarounds are flakey.

from silverstripe-framework.

tractorcow avatar tractorcow commented on July 18, 2024

Is there a way to flag issues on Github, or can someone assign this issue to me?

from silverstripe-framework.

simonwelsh avatar simonwelsh commented on July 18, 2024

Issues can only be assigned to people in the organisation.

from silverstripe-framework.

tractorcow avatar tractorcow commented on July 18, 2024

Pull request is in the CMS module, including test cases. I've resolved the flakiness issue by adding a $parentClass specifier, allowing user code to tweak the behaviour where otherwise errors or infinite loops would occur.

from silverstripe-framework.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.