trilbymedia / grav-plugin-flex-objects

Powerful and supremely flexible object support for Grav
MIT License
41 stars 10 forks source link

Flex collection is empty when defined in theme (Call to a member function render() on null) #177

Open LiaraAlis opened 1 year ago

LiaraAlis commented 1 year ago

Hello together,

after updating to flex objects 1.3.5 I got the following error when opening pages with flex collections:

Error thrown with message "Call to a member function render() on null"

Stacktrace:
#71 Error in /home/apankow/Code/noruken-dojo/cache/twig/12/12559536e441581ca34f6fdfacdad216af4d7726473ab10ee24c5b0369ef9a1f.php:113
#70 __TwigTemplate_6ae79ba66a652e40a412d47e8c1b2db7aa811e6c7819fe581d910d6e2459e6af:doDisplay in /home/apankow/Code/noruken-dojo/vendor/twig/twig/src/Template.php:453
#69 Twig\Template:displayWithErrorHandling in /home/apankow/Code/noruken-dojo/vendor/twig/twig/src/Template.php:420
#68 Twig\Template:display in /home/apankow/Code/noruken-dojo/vendor/twig/twig/src/Template.php:432
#67 Twig\Template:render in /home/apankow/Code/noruken-dojo/vendor/twig/twig/src/TemplateWrapper.php:47
#66 Twig\TemplateWrapper:render in /home/apankow/Code/noruken-dojo/vendor/twig/twig/src/Environment.php:384
#65 Twig\Environment:render in /home/apankow/Code/noruken-dojo/system/src/Grav/Common/Twig/Twig.php:338
#64 Grav\Common\Twig\Twig:processPage in /home/apankow/Code/noruken-dojo/system/src/Grav/Common/Page/Page.php:974
#63 Grav\Common\Page\Page:processTwig in /home/apankow/Code/noruken-dojo/system/src/Grav/Common/Page/Page.php:820
#62 Grav\Common\Page\Page:content in /home/apankow/Code/noruken-dojo/vendor/twig/twig/src/Template.php:700
#61 Twig\Template:getAttribute in /home/apankow/Code/noruken-dojo/cache/twig/83/8359441c29d7565613918cc81bb0a3e1b76cb442cebe1ca28eb236d4982a2eb5.php:91
#60 __TwigTemplate_3c29b817c6678d969b6afcc050884b2600760e7b4e7cbf5d004836edef1939c4:block_content in /home/apankow/Code/noruken-dojo/vendor/twig/twig/src/Template.php:214
#59 Twig\Template:displayBlock in /home/apankow/Code/noruken-dojo/cache/twig/83/8359441c29d7565613918cc81bb0a3e1b76cb442cebe1ca28eb236d4982a2eb5.php:58
#58 __TwigTemplate_3c29b817c6678d969b6afcc050884b2600760e7b4e7cbf5d004836edef1939c4:block_body in /home/apankow/Code/noruken-dojo/vendor/twig/twig/src/Template.php:214
#57 Twig\Template:displayBlock in /home/apankow/Code/noruken-dojo/cache/twig/5f/5f42e1773c8e98d85999ba354f1bc9fd6ec316874e4087b53c5dc392260df793.php:206
#56 __TwigTemplate_ac9ec807b77f2f962f082f5f457c175680fab8e91b79842e55d05f0a03ac1d47:block_wrapper in /home/apankow/Code/noruken-dojo/vendor/twig/twig/src/Template.php:214
#55 Twig\Template:displayBlock in /home/apankow/Code/noruken-dojo/cache/twig/5f/5f42e1773c8e98d85999ba354f1bc9fd6ec316874e4087b53c5dc392260df793.php:79
#54 __TwigTemplate_ac9ec807b77f2f962f082f5f457c175680fab8e91b79842e55d05f0a03ac1d47:doDisplay in /home/apankow/Code/noruken-dojo/vendor/twig/twig/src/Template.php:453
#53 Twig\Template:displayWithErrorHandling in /home/apankow/Code/noruken-dojo/vendor/twig/twig/src/Template.php:420
#52 Twig\Template:display in /home/apankow/Code/noruken-dojo/cache/twig/83/8359441c29d7565613918cc81bb0a3e1b76cb442cebe1ca28eb236d4982a2eb5.php:39
#51 __TwigTemplate_3c29b817c6678d969b6afcc050884b2600760e7b4e7cbf5d004836edef1939c4:doDisplay in /home/apankow/Code/noruken-dojo/vendor/twig/twig/src/Template.php:453
#50 Twig\Template:displayWithErrorHandling in /home/apankow/Code/noruken-dojo/vendor/twig/twig/src/Template.php:420
#49 Twig\Template:display in /home/apankow/Code/noruken-dojo/vendor/twig/twig/src/Template.php:432
#48 Twig\Template:render in /home/apankow/Code/noruken-dojo/vendor/twig/twig/src/TemplateWrapper.php:47
#47 Twig\TemplateWrapper:render in /home/apankow/Code/noruken-dojo/vendor/twig/twig/src/Environment.php:384
#46 Twig\Environment:render in /home/apankow/Code/noruken-dojo/system/src/Grav/Common/Twig/Twig.php:448
#45 Grav\Common\Twig\Twig:processSite in /home/apankow/Code/noruken-dojo/system/src/Grav/Common/Service/OutputServiceProvider.php:36
#44 Grav\Common\Service\OutputServiceProvider:Grav\Common\Service\{closure} in /home/apankow/Code/noruken-dojo/vendor/pimple/pimple/src/Pimple/Container.php:122
#43 Pimple\Container:offsetGet in /home/apankow/Code/noruken-dojo/system/src/Grav/Common/Processors/RenderProcessor.php:40
#42 Grav\Common\Processors\RenderProcessor:process in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#41 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#40 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Common/Processors/DebuggerAssetsProcessor.php:38
#39 Grav\Common\Processors\DebuggerAssetsProcessor:process in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#38 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#37 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Common/Processors/PagesProcessor.php:113
#36 Grav\Common\Processors\PagesProcessor:process in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#35 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#34 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Common/Processors/TwigProcessor.php:38
#33 Grav\Common\Processors\TwigProcessor:process in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#32 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#31 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Common/Processors/AssetsProcessor.php:39
#30 Grav\Common\Processors\AssetsProcessor:process in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#29 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#28 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Common/Processors/SchedulerProcessor.php:40
#27 Grav\Common\Processors\SchedulerProcessor:process in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#26 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#25 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Common/Processors/BackupsProcessor.php:39
#24 Grav\Common\Processors\BackupsProcessor:process in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#23 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#22 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Common/Processors/TasksProcessor.php:69
#21 Grav\Common\Processors\TasksProcessor:process in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#20 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#19 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Common/Processors/RequestProcessor.php:64
#18 Grav\Common\Processors\RequestProcessor:process in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#17 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#16 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Common/Processors/ThemesProcessor.php:38
#15 Grav\Common\Processors\ThemesProcessor:process in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#14 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#13 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Common/Processors/PluginsProcessor.php:39
#12 Grav\Common\Processors\PluginsProcessor:process in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#11 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#10 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Common/Processors/InitializeProcessor.php:130
#9 Grav\Common\Processors\InitializeProcessor:Grav\Common\Processors\{closure} in /home/apankow/Code/noruken-dojo/system/src/Grav/Common/Debugger.php:546
#8 Grav\Common\Debugger:profile in /home/apankow/Code/noruken-dojo/system/src/Grav/Common/Processors/InitializeProcessor.php:129
#7 Grav\Common\Processors\InitializeProcessor:process in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#6 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#5 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Middlewares/MultipartRequestSupport.php:40
#4 Grav\Framework\RequestHandler\Middlewares\MultipartRequestSupport:process in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#3 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#2 Grav\Framework\RequestHandler\RequestHandler:handle in /home/apankow/Code/noruken-dojo/system/src/Grav/Common/Grav.php:312
#1 Grav\Common\Grav:process in /home/apankow/Code/noruken-dojo/index.php:47
#0 require in /home/apankow/.symfony/php/06668e36a5a271a282c77dc7da4828f5f9c957f3-router.php:29

My blueprint is defined in theme and is listed in plugin settings and activated. The flex objects are shown in admin and can be maintained there without any problems.

In my template I want to render the collection layout:

            {% set coaches = grav.get('flex').collection('coaches') %}
            {% render coaches layout: 'cards' %}

But grav.get('flex').collection('coaches') is always null. It works when moving the flex-objects to user directory.

LiaraAlis commented 1 year ago

I experimented a little. It seems to be related to the Grav version. If I downgrade the core to 1.7.40, it works again. From version 1.7.41 the error occurs.

LiaraAlis commented 1 year ago

Is this plugin still alive? Nothing happens since Jul 9!

rhukster commented 1 year ago

Not enough information to do any debugging from.

LiaraAlis commented 1 year ago

Not enough information to do any debugging from.

Okay, is there something I can do to help with this? :)

rhukster commented 1 year ago

You should provide as much information as possible. What did you do for this error to occur? What plugins do you have installed? what versions are they? what version of PHP? How can I recreate? Can you provide a simple setup i can unzip and test with (this should include custom templates, blueprints, configuration, etc).

I need to be able to replicate the issue exactly or I can't help you.

Cheers,

LiaraAlis commented 10 months ago

@rhukster I added a new blueprint type and before grav version 1.7.41 it works without problems. Starting with this version, blueprint is no longer found in the frontend.

image

404 is thrown from template flex-objects/views/directory.html.twig because variable directory is null for the type projects.

After a long research it seems that the path to the theme blueprint directory is only added to the locator if the flex object plugin has already tried to add the directory to the flex object for the respective type. However, since it cannot find the blueprint, the listing in the frontend with custom blueprint types does not work. Asking the locator about the blueprint path after theme initialization is finished, produces a positive result - but it is too late for the flex object when using Flex::getDirectory().

Mentioned also in https://github.com/getgrav/grav/issues/3255#issuecomment-1876015476.

LiaraAlis commented 10 months ago

I found the reason. Because in system/blueprints/user/account.yaml flex objects are used to load group names, flex is initialized way before expected and for this reason, the registration of additional path in locator does not have any effect.

Changing the following value back solves the problem: system/blueprints/user/account.yaml

title: Account
form:
    validation: loose

    fields:

        info:
            type: userinfo
            size: large

        avatar:
            type: file
            size: large
            destination: 'account://avatars'
            multiple: false
            random_name: true

        multiavatar_only:
          type: conditional
          condition: config.system.accounts.avatar == 'multiavatar'
          fields:
            avatar_hash:
                type: text
                label: ''
                placeholder: 'e.g. dceaadcfda491f4e45'
                description: PLUGIN_ADMIN.AVATAR_HASH
                size: large

        content:
            type: section
            title: PLUGIN_ADMIN.ACCOUNT
            underline: true

        username:
            type: text
            size: large
            label: PLUGIN_ADMIN.USERNAME
            disabled: true
            readonly: true

        email:
            type: email
            size: large
            label: PLUGIN_ADMIN.EMAIL
            validate:
              type: email
              message: PLUGIN_ADMIN.EMAIL_VALIDATION_MESSAGE
              required: true

        password:
            type: password
            size: large
            label: PLUGIN_ADMIN.PASSWORD
            autocomplete: new-password
            validate:
              required: false
              message: PLUGIN_ADMIN.PASSWORD_VALIDATION_MESSAGE
              config-pattern@: system.pwd_regex

        fullname:
            type: text
            size: large
            label: PLUGIN_ADMIN.FULL_NAME
            validate:
              required: true

        title:
            type: text
            size: large
            label: PLUGIN_ADMIN.TITLE

        language:
            type: select
            label: PLUGIN_ADMIN.LANGUAGE
            size: medium
            classes: fancy
            data-options@: '\Grav\Plugin\Admin\Admin::adminLanguages'
            default: 'en'
            help: PLUGIN_ADMIN.LANGUAGE_HELP

        content_editor:
            type: select
            label: PLUGIN_ADMIN.CONTENT_EDITOR
            size: medium
            classes: fancy
            data-options@: 'Grav\Plugin\Admin\Admin::contentEditor'
            default: 'default'
            help: PLUGIN_ADMIN.CONTENT_EDITOR_HELP

        twofa_check:
            type: conditional
            condition: config.plugins.admin.twofa_enabled

            fields:

                twofa:
                    title: PLUGIN_ADMIN.2FA_TITLE
                    type: section
                    underline: true

                twofa_enabled:
                    type: toggle
                    label: PLUGIN_ADMIN.2FA_ENABLED
                    classes: twofa-toggle
                    highlight: 1
                    default: 0
                    options:
                      1: PLUGIN_ADMIN.YES
                      0: PLUGIN_ADMIN.NO
                    validate:
                      type: bool

                twofa_secret:
                    type: 2fa_secret
                    outerclasses: 'twofa-secret'
                    markdown: true
                    label: PLUGIN_ADMIN.2FA_SECRET
                    sublabel: PLUGIN_ADMIN.2FA_SECRET_HELP

                yubikey_id:
                    type: text
                    label: PLUGIN_ADMIN.YUBIKEY_ID
                    description: PLUGIN_ADMIN.YUBIKEY_HELP
                    size: small
                    maxlength: 12

        security:
            security@: admin.super
            title: PLUGIN_ADMIN.ACCESS_LEVELS
            type: section
            underline: true

            fields:
                groups:
                    security@: admin.super
                    type: select
                    multiple: true
                    size: large
                    label: PLUGIN_ADMIN.GROUPS
-                    data-options@: 'Grav\Common\Flex\Types\UserGroups\UserGroupObject::groupNames'
+                    data-options@: '\Grav\Common\User\Group::groupNames'
                    classes: fancy
                    help: PLUGIN_ADMIN.GROUPS_HELP
                    validate:
                        type: commalist

                access:
                    security@: admin.super
                    type: permissions
                    check_authorize: true
                    label: PLUGIN_ADMIN.PERMISSIONS
                    ignore_empty: true
                    validate:
                        type: array
                        value_type: bool

Also, I was able to dirty-fix it in the template:

{% set flex = grav.get('flex_objects') %}
+ {# fixed bug https://github.com/trilbymedia/grav-plugin-flex-objects/issues/177 #}
+ {% do flex.addDirectoryType('projects', 'blueprints://flex-objects/projects.yaml') %}
{% set directory = flex.directory(type) %}
rhukster commented 10 months ago

I don't remember why i changed this now.. reveverted until I can work out why it was changed and what the proper fix would be.

LiaraAlis commented 10 months ago

Can confirm it works again with grav v1.7.44.