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

Flex Object with file field not working #37

Closed
chesjon opened this issue Nov 11, 2019 · 8 comments
Closed

Flex Object with file field not working #37

chesjon opened this issue Nov 11, 2019 · 8 comments
Assignees
Labels
bug Something isn't working fixed Issue has already been fixed

Comments

@chesjon
Copy link

chesjon commented Nov 11, 2019

I am having trouble getting a field of type file working within a flex object blueprint. In the administration panel I am presented with the option to select a file but when I save the file is not there as if it did not upload. The value for the file field in the markdown is also empty:

---
name: 'Fraser Fir'
price: '60'
description: 'Test Description'
img: {  }
---

Here is my flex object blueprint

title: Trees
type: flex-objects

# Flex Configuration
config:
  # Administration Configuration
  admin:
    # Admin menu (optional)
    menu:
      list:
        route: '/trees'
        title: Trees
        icon: fa-address-card-o
        # Authorization to collection admin
        authorize: ['admin.pages', 'admin.super']
        # Priority -10 .. 10 (highest goes up)
        priority: 2

    # Admin template type / folder
    template: default

    # List view
    list:
      title: name
      fields:
        header.name:
          link: edit
        header.price:
          link: edit
        header.description:
          link: edit

    # Edit View
    edit:
      title:
        template: '{{ object.name ?? ''Tree'' }}'

    # Data Export
    export:
      enabled: true
      method: 'jsonSerialize'
      formatter:
        class: 'Grav\Framework\File\Formatter\YamlFormatter'
      filename: 'trees'

  # Site Configuration
  site:
    templates:
      collection:
        # Lookup for the template layout files for collections of objects
        paths:
          - 'flex/{TYPE}/collection/{LAYOUT}{EXT}'
      object:
        # Lookup for the template layout files for objects
        paths:
          - 'flex/{TYPE}/object/{LAYOUT}{EXT}'
      defaults:
        # Default template {TYPE}; overridden by filename of this blueprint if template folder exists
        type: trees
        # Default template {LAYOUT}; can be overridden in render calls (usually Twig in templates)
        layout: default

  # Data Configuration
  data:
    # Object class to be used, allowing custom methods for the object
    object: 'Grav\Framework\Flex\FlexObject'
    # Collection class to be used, allowing custom methods for the collections
    collection: 'Grav\Framework\Flex\FlexCollection'
    # Index class to be used, works as a quick database-like lookup index
    index: 'Grav\Framework\Flex\FlexIndex'
    storage:
      # Storage class, if you want to have associated assets, such like images, use FolderStorage
      class: 'Grav\Framework\Flex\Storage\FolderStorage'
      options:
        formatter:
          # File formatter class, in this case the file is stored in markdown
          class: 'Grav\Framework\File\Formatter\MarkdownFormatter'
        # Folder where all the objects will be stored
        folder: user-data://flex-objects
        # File pattern to generate the filename
        pattern: '{FOLDER}/{KEY}/tree{EXT}'
        # Field which will be used as {KEY}
        key: storage_key
        # Keep index file in filesystem to speed up lookups
        indexed: true
    search:
      # Search options
      options:
        contains: 1
      # Fields to be searched
      fields:
        - header.name
        - header.price
        - header.description

form:
  validation: loose

  fields:

    header.name:
      type: text
      label: Name
      validate:
        required: true

    header.price:
      type: text
      label: Price
      validate:
        required: true

    header.description:
      type: textarea
      label: Description
      validate:
        required: true

    header.img:
      label: Tree Image
      type: file
      multiple: false
      accept:
        - 'image/*'

Grav version: 1.7.0-rc.1
Admin: 1.10.0-rc.1
Form Plugin: 4.0.0

I have tried with and without the destination defined for the header.img field.

I am hoping someone can help me identify whether this is a bug with the flex objects or if I am doing something wrong.

Thank you!

@mahagr
Copy link
Contributor

mahagr commented Nov 11, 2019

RIght now file field needs special support from the object type (as in code) and is not supported without writing your own object class. This is mostly because of I've not had time to look into more general option yet.

@mahagr mahagr self-assigned this Nov 11, 2019
@mahagr mahagr added the enhancement Enhancement for an existing feature label Nov 11, 2019
@chesjon
Copy link
Author

chesjon commented Nov 11, 2019

Is there an example I can follow to write my own object class for the file field? If you can point me in the right direction that would be great

@mahagr
Copy link
Contributor

mahagr commented Nov 12, 2019

Both user and page class has example in it. They are quite specialized, but should give some idea how I'm doing it right now.

@dwr-studio
Copy link

Hi, I'm trying to follow some examples inside the user and page classes but I can't figure out where these classes are stored. Am I right to look inside the src/Grav/Common/Flex/ folder?
Thanks for helping, sorry for the stupid question.

@SeriousKen
Copy link

I'm also struggling to figure out what to implement in my own class to make file uploads work. Any help would be great.

@rhukster
Copy link
Member

rhukster commented Dec 8, 2019

You need to create your own plugin and put the files under the folder structure in there. Don't put anything inside the flex-objects plugin folder it will get lost when you update it.

We probably need to do a blog post or something that walks you through the process.

@SeriousKen
Copy link

@rhukster I've got that far, it's implementing the file upload within the class, ie which methods do I overload and how do I save/delete files as necessary.

@mahagr mahagr added the fixed Issue has already been fixed label Jun 4, 2020
@mahagr
Copy link
Contributor

mahagr commented Jun 4, 2020

File field has been fixed as long as you use or extend GenericObject or use the provided types.

@mahagr mahagr added bug Something isn't working and removed enhancement Enhancement for an existing feature labels Jun 4, 2020
@mahagr mahagr closed this as completed Jun 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed Issue has already been fixed
Projects
None yet
Development

No branches or pull requests

5 participants