getgrav / grav

Modern, Crazy Fast, Ridiculously Easy and Amazingly Powerful Flat-File CMS powered by PHP, Markdown, Twig, and Symfony
https://getgrav.org
MIT License
14.57k stars 1.41k forks source link

Pagemediaselect form field not working when nested inside elements form field #3584

Open bnjmnhssnn opened 2 years ago

bnjmnhssnn commented 2 years ago

I have an issue when nesting e pagemediaselect form field inside an elements form field. This is my test setup to repoduce the issue, tested with Grav (1.7.33):

title: Test
extends@:
  type: default
  context: blueprints://pages

form:
  validation: loose

  fields:

    tabs:
      type: tabs
      active: 1

      fields:

        content:
          type: tab
          title: PLUGIN_ADMIN.CONTENT
          fields:
            header.foo.bar.image_1:
              type: pagemediaselect
              label: 'This works'
              preview_images: true

            header.bar.type:
              type: elements
              label: 'Elements Test'
              size: small
              default: baz
              options:
                baz: Baz
              fields:
                baz:
                  type: element
                  fields:
                    .image_2:
                      type: pagemediaselect
                      label: 'This not'
                      preview_images: true

Clicking the first, working pagemediaselect input triggers an ajax request to:

http://localhost/admin/pages/my-page.json/task:getFilesInFolder

The payload looks like this:

admin-nonce: cf4fede4[...]
name: header.foo.bar.image_1
params: {}

This works fine and the admin panel responds with the expected JSON:

{"code":200,"status":"success","files":["my-image.jpg"], ...

Clicking the second, nested pagemediaselect input triggers an ajax request to the same URL:

http://localhost/admin/pages/my-page.json/task:getFilesInFolder

The payload is structured exactly like the one triggered by the un-nested pagemediaselect:

admin-nonce: cf4fede4[...]
name: header.bar.baz.image_2
params: {}

Strangely, this does not work, although the request URL and payload are not different:


{
    "code": 404,
    "status": "error",
    "message": "Not Found",
    "redirect": null,
    "error": {
        "code": 404,
        "message": "Not Found",
        "type": "RuntimeException",
        "file": "\/var\/www\/web\/user\/plugins\/flex-objects\/classes\/Controllers\/MediaController.php",
        "line": 385,
        "trace": [
            "#0 \/var\/www\/web\/user\/plugins\/flex-objects\/classes\/Controllers\/AbstractController.php(127): Grav\\Plugin\\FlexObjects\\Controllers\\MediaController->actionMediaPicker(Object(Nyholm\\Psr7\\ServerRequest))",
            "#1 \/var\/www\/web\/user\/plugins\/flex-objects\/classes\/Admin\/AdminController.php(1226): Grav\\Plugin\\FlexObjects\\Controllers\\AbstractController->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#2 \/var\/www\/web\/user\/plugins\/flex-objects\/classes\/Admin\/AdminController.php(1192): Grav\\Plugin\\FlexObjects\\Admin\\AdminController->forwardMediaTask('action', 'media.picker')",
            "#3 \/var\/www\/web\/user\/plugins\/flex-objects\/classes\/Admin\/AdminController.php(1443): Grav\\Plugin\\FlexObjects\\Admin\\AdminController->taskGetFilesInFolder()",
            "#4 \/var\/www\/web\/user\/plugins\/flex-objects\/flex-objects.php(587): Grav\\Plugin\\FlexObjects\\Admin\\AdminController->execute()",
            "#5 \/var\/www\/web\/vendor\/symfony\/event-dispatcher\/EventDispatcher.php(264): Grav\\Plugin\\FlexObjectsPlugin->onAdminPageInitialized(Object(RocketTheme\\Toolbox\\Event\\Event), 'onPageInitializ...', Object(Symfony\\Component\\EventDispatcher\\EventDispatcher))",
            "#6 \/var\/www\/web\/vendor\/symfony\/event-dispatcher\/EventDispatcher.php(239): Symfony\\Component\\EventDispatcher\\EventDispatcher->doDispatch(Array, 'onPageInitializ...', Object(RocketTheme\\Toolbox\\Event\\Event))",
            "#7 \/var\/www\/web\/vendor\/symfony\/event-dispatcher\/EventDispatcher.php(73): Symfony\\Component\\EventDispatcher\\EventDispatcher->callListeners(Array, 'onPageInitializ...', Object(RocketTheme\\Toolbox\\Event\\Event))",
            "#8 \/var\/www\/web\/system\/src\/Grav\/Common\/Grav.php(592): Symfony\\Component\\EventDispatcher\\EventDispatcher->dispatch(Object(RocketTheme\\Toolbox\\Event\\Event), 'onPageInitializ...')",
            "#9 \/var\/www\/web\/system\/src\/Grav\/Common\/Processors\/PagesProcessor.php(59): Grav\\Common\\Grav->fireEvent('onPageInitializ...', Object(RocketTheme\\Toolbox\\Event\\Event))",
            "#10 \/var\/www\/web\/system\/src\/Grav\/Framework\/RequestHandler\/Traits\/RequestHandlerTrait.php(50): Grav\\Common\\Processors\\PagesProcessor->process(Object(Nyholm\\Psr7\\ServerRequest), Object(Grav\\Framework\\RequestHandler\\RequestHandler))",
            "#11 \/var\/www\/web\/system\/src\/Grav\/Framework\/RequestHandler\/Traits\/RequestHandlerTrait.php(62): Grav\\Framework\\RequestHandler\\RequestHandler->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#12 \/var\/www\/web\/system\/src\/Grav\/Common\/Processors\/TwigProcessor.php(38): Grav\\Framework\\RequestHandler\\RequestHandler->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#13 \/var\/www\/web\/system\/src\/Grav\/Framework\/RequestHandler\/Traits\/RequestHandlerTrait.php(50): Grav\\Common\\Processors\\TwigProcessor->process(Object(Nyholm\\Psr7\\ServerRequest), Object(Grav\\Framework\\RequestHandler\\RequestHandler))",
            "#14 \/var\/www\/web\/system\/src\/Grav\/Framework\/RequestHandler\/Traits\/RequestHandlerTrait.php(62): Grav\\Framework\\RequestHandler\\RequestHandler->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#15 \/var\/www\/web\/system\/src\/Grav\/Common\/Processors\/AssetsProcessor.php(39): Grav\\Framework\\RequestHandler\\RequestHandler->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#16 \/var\/www\/web\/system\/src\/Grav\/Framework\/RequestHandler\/Traits\/RequestHandlerTrait.php(50): Grav\\Common\\Processors\\AssetsProcessor->process(Object(Nyholm\\Psr7\\ServerRequest), Object(Grav\\Framework\\RequestHandler\\RequestHandler))",
            "#17 \/var\/www\/web\/system\/src\/Grav\/Framework\/RequestHandler\/Traits\/RequestHandlerTrait.php(62): Grav\\Framework\\RequestHandler\\RequestHandler->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#18 \/var\/www\/web\/system\/src\/Grav\/Common\/Processors\/SchedulerProcessor.php(40): Grav\\Framework\\RequestHandler\\RequestHandler->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#19 \/var\/www\/web\/system\/src\/Grav\/Framework\/RequestHandler\/Traits\/RequestHandlerTrait.php(50): Grav\\Common\\Processors\\SchedulerProcessor->process(Object(Nyholm\\Psr7\\ServerRequest), Object(Grav\\Framework\\RequestHandler\\RequestHandler))",
            "#20 \/var\/www\/web\/system\/src\/Grav\/Framework\/RequestHandler\/Traits\/RequestHandlerTrait.php(62): Grav\\Framework\\RequestHandler\\RequestHandler->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#21 \/var\/www\/web\/system\/src\/Grav\/Common\/Processors\/BackupsProcessor.php(39): Grav\\Framework\\RequestHandler\\RequestHandler->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#22 \/var\/www\/web\/system\/src\/Grav\/Framework\/RequestHandler\/Traits\/RequestHandlerTrait.php(50): Grav\\Common\\Processors\\BackupsProcessor->process(Object(Nyholm\\Psr7\\ServerRequest), Object(Grav\\Framework\\RequestHandler\\RequestHandler))",
            "#23 \/var\/www\/web\/system\/src\/Grav\/Framework\/RequestHandler\/Traits\/RequestHandlerTrait.php(62): Grav\\Framework\\RequestHandler\\RequestHandler->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#24 \/var\/www\/web\/system\/src\/Grav\/Common\/Processors\/TasksProcessor.php(69): Grav\\Framework\\RequestHandler\\RequestHandler->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#25 \/var\/www\/web\/system\/src\/Grav\/Framework\/RequestHandler\/Traits\/RequestHandlerTrait.php(50): Grav\\Common\\Processors\\TasksProcessor->process(Object(Nyholm\\Psr7\\ServerRequest), Object(Grav\\Framework\\RequestHandler\\RequestHandler))",
            "#26 \/var\/www\/web\/system\/src\/Grav\/Framework\/RequestHandler\/Traits\/RequestHandlerTrait.php(62): Grav\\Framework\\RequestHandler\\RequestHandler->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#27 \/var\/www\/web\/user\/plugins\/admin\/classes\/plugin\/Router.php(65): Grav\\Framework\\RequestHandler\\RequestHandler->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#28 \/var\/www\/web\/system\/src\/Grav\/Framework\/RequestHandler\/Traits\/RequestHandlerTrait.php(50): Grav\\Plugin\\Admin\\Router->process(Object(Nyholm\\Psr7\\ServerRequest), Object(Grav\\Framework\\RequestHandler\\RequestHandler))",
            "#29 \/var\/www\/web\/system\/src\/Grav\/Framework\/RequestHandler\/Traits\/RequestHandlerTrait.php(62): Grav\\Framework\\RequestHandler\\RequestHandler->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#30 \/var\/www\/web\/system\/src\/Grav\/Common\/Processors\/RequestProcessor.php(64): Grav\\Framework\\RequestHandler\\RequestHandler->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#31 \/var\/www\/web\/system\/src\/Grav\/Framework\/RequestHandler\/Traits\/RequestHandlerTrait.php(50): Grav\\Common\\Processors\\RequestProcessor->process(Object(Nyholm\\Psr7\\ServerRequest), Object(Grav\\Framework\\RequestHandler\\RequestHandler))",
            "#32 \/var\/www\/web\/system\/src\/Grav\/Framework\/RequestHandler\/Traits\/RequestHandlerTrait.php(62): Grav\\Framework\\RequestHandler\\RequestHandler->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#33 \/var\/www\/web\/system\/src\/Grav\/Common\/Processors\/ThemesProcessor.php(38): Grav\\Framework\\RequestHandler\\RequestHandler->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#34 \/var\/www\/web\/system\/src\/Grav\/Framework\/RequestHandler\/Traits\/RequestHandlerTrait.php(50): Grav\\Common\\Processors\\ThemesProcessor->process(Object(Nyholm\\Psr7\\ServerRequest), Object(Grav\\Framework\\RequestHandler\\RequestHandler))",
            "#35 \/var\/www\/web\/system\/src\/Grav\/Framework\/RequestHandler\/Traits\/RequestHandlerTrait.php(62): Grav\\Framework\\RequestHandler\\RequestHandler->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#36 \/var\/www\/web\/system\/src\/Grav\/Common\/Processors\/PluginsProcessor.php(39): Grav\\Framework\\RequestHandler\\RequestHandler->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#37 \/var\/www\/web\/system\/src\/Grav\/Framework\/RequestHandler\/Traits\/RequestHandlerTrait.php(50): Grav\\Common\\Processors\\PluginsProcessor->process(Object(Nyholm\\Psr7\\ServerRequest), Object(Grav\\Framework\\RequestHandler\\RequestHandler))",
            "#38 \/var\/www\/web\/system\/src\/Grav\/Framework\/RequestHandler\/Traits\/RequestHandlerTrait.php(62): Grav\\Framework\\RequestHandler\\RequestHandler->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#39 \/var\/www\/web\/system\/src\/Grav\/Common\/Processors\/InitializeProcessor.php(130): Grav\\Framework\\RequestHandler\\RequestHandler->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#40 \/var\/www\/web\/system\/src\/Grav\/Common\/Debugger.php(546): Grav\\Common\\Processors\\InitializeProcessor::Grav\\Common\\Processors\\{closure}()",
            "#41 \/var\/www\/web\/system\/src\/Grav\/Common\/Processors\/InitializeProcessor.php(131): Grav\\Common\\Debugger->profile(Object(Closure))",
            "#42 \/var\/www\/web\/system\/src\/Grav\/Framework\/RequestHandler\/Traits\/RequestHandlerTrait.php(50): Grav\\Common\\Processors\\InitializeProcessor->process(Object(Nyholm\\Psr7\\ServerRequest), Object(Grav\\Framework\\RequestHandler\\RequestHandler))",
            "#43 \/var\/www\/web\/system\/src\/Grav\/Framework\/RequestHandler\/Traits\/RequestHandlerTrait.php(62): Grav\\Framework\\RequestHandler\\RequestHandler->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#44 \/var\/www\/web\/system\/src\/Grav\/Common\/Grav.php(307): Grav\\Framework\\RequestHandler\\RequestHandler->handle(Object(Nyholm\\Psr7\\ServerRequest))",
            "#45 \/var\/www\/web\/index.php(52): Grav\\Common\\Grav->process()",
            "#46 {main}"
        ]
    }
}
bnjmnhssnn commented 2 years ago

I was able to get this test setup working by changing the pagemediaselect field name in the blueprint from .image_2 to header.bar.image_2.

However, this is not a solution when the elements field is nested in another list field. I believe a final solution should work inside lists and also with partial blueprints and include@

HiddenType commented 2 years ago

I ran into the exact issue @bnjmnhssnn predicted in his second post.

While nesting filepicker (or pagemedia) in elements OR list is possible, nesting them in elements IN a list is not. A warning message «Not found» appears in the top right corner. (Tested in Grav v1.7.33 w/ Admin v1.10.33.1)

form:
  fields:
    tabs:
      fields:
        content:
          fields:
            content:
              unset@: true
            header.list:
              type: list
              label: List
              style: vertical
              fields:
                .name:
                  type: text
                  label: Name
                .pagemedia:
                  type: pagemediaselect
                  label: Pagemediaselect in List
                  preview_images: true
                .file:
                  type: filepicker
                  folder: 'page://images/'
                  label: Filepicker in List
                  preview_images: true
                  accept:
                    - .jpg
                    - .png
                .type:
                  type: elements
                  label: 'Type'
                  size: small
                  default: text_only
                  options:
                    text_only: Text only
                    text_and_image: Text and image
                  fields:
                    text_only:
                      type: element
                      fields:
                        .text:
                          type: text
                          label: Text (One)
                    text_and_image:
                      type: element
                      fields:
                        .text:
                          type: text
                          label: Text (Two)
                        .file:
                          type: filepicker
                          folder: 'page://images/'
                          label: Filepicker in List in Elements
                          preview_images: true
                          accept:
                            - .jpg
                            - .png
                        .pagemedia:
                          type: pagemediaselect
                          label: Pagemediaselect in List in Elements
                          preview_images: true
jovaniedelacruz commented 11 months ago

I am running into this exact problem with a pagemedia selected inside an elements nested in a list. Anyone else find a workaround for this? Any reason it shouldn't work since the payloads are the same? Gonna try with the include@ method maybe that will someone fix it.

Edit: Just tried wrapping my elements section in a partial blueprint and using include@ but it still results in the same 404 Not Found Error for pagemediaselect