jamestagal / plenti-educenter

0 stars 1 forks source link

Setup of entity reference (Autocomplete) field to reference existing teachers when adding/editing courses #13

Closed jamestagal closed 1 year ago

jamestagal commented 1 year ago

Hi @jimafisk

I have set up an entity reference field in the _schema.json file in courses content type as follows:

{
    "trainer.name": {
      "type": "autocomplete",
      "options": [
        {
          "type": "teachers",
          "search": [
            "title"
          ],
          "result": "fieldname"
        }
      ]  
    }
 } 

As you can see, I am referencing the trainer.name field in the courses content type to target the title field in the teachers content type. This mostly works however I don't really know all the types of results I can get back other than filename one you used in your example. I was hoping for fieldname but I get back undefined. See screencast.

https://user-images.githubusercontent.com/6309595/211805367-79276a19-895c-46c8-b5ac-944ca8687e88.mov

I want to get the title of the teachers back but not sure how to do that.

Pls let me know how I could get the title of the teachers back in the reference.

Best regards, Ben

UPDATE: I found the list of types in the Docs and tried setting the result to fields but that did not work either and undefined is returned. I even tried "fields.title" but that didn't work.

 "result": "fields"
jamestagal commented 1 year ago

Hi @jimafisk

Setting the path does get a result such as teacher/clark-malikwhich is good. However I think I only want the title field i.e. the teacher name to be returned because under the hood we are rendering both the path and title fields and filtering to match the trainer.name in the courses content type with the title field of the teachers content type to dynamically display teacher titles and paths here.

In courses.svelte

   <a href="{teacher.path}">
       {teacher.fields.title}
   </a>
jimafisk commented 1 year ago

That's an interesting concept, currently it only exposes the top-level metadata for the content:

But it would be nice if you could dig into a particular field with dot notation like:

{
  "trainer.name": {
    "type": "autocomplete",
    "options": [
      {
        "type": "teachers",
        "search": [
          "title"
        ],
        "result": "fields.title"
      }
    ]
  }
}

(also I'm pushing some API updates (https://github.com/jamestagal/plenti-educenter/commit/258b5c715097bdfa177d3373c13f00c0c6e9ec56) to this repo for the v0.6.x version of Plenti. You'll need to upgrade your local version to run it. Let me know if you have any questions on that!

jamestagal commented 1 year ago

Hi @jimafisk Thanks very much for all those updates...I will upgrade Plenti now. Also, how do you do single word changes like this efficiently? Something like find and replace? Would be nice to know how to do it. šŸ˜Š

But it would be nice if you could dig into a particular field with dot notation like:

Agreed because the fields are all there, they just need to be exposed to become accessible. Maybe another one for the To Do list?

Ben

jamestagal commented 1 year ago

Hi @jimafisk ,

Just a note on the entity reference field. When adding it in the way I did above, trainer.name fields, I noticed when I click the edit button those two field including the image temporarily disappear in the UI but come back once I exit the editor and reload the page. See screencast illustrating he behaviour below.

https://user-images.githubusercontent.com/6309595/211965045-b2f54e9c-7303-4654-a688-2a44f720db2c.mov

jimafisk commented 1 year ago

Also, how do you do single word changes like this efficiently?

I used find . -type f -exec sed -i 's/assets/media/g' {} \; from https://stackoverflow.com/questions/8905662/how-to-change-all-occurrences-of-a-word-in-all-files-in-a-directory

But... I wasn't careful and this broke my .git so I needed to rm -f .git/index and git reset from https://stackoverflow.com/questions/47109232/git-fatal-unknown-index-entry-format

Agreed because the fields are all there, they just need to be exposed to become accessible. Maybe another one for the To Do list?

You inspired me! I just pushed a fix for this (it won't be available until the next release though): https://github.com/plentico/plenti/commit/da5a0b25f82799b4374ecf04e641b7b8b4e47f00

jamestagal commented 1 year ago

Hi @jimafisk Wow. That's really cool. Well I'm happy to return the inspiration because Plenti has inspired me! Looking forward to the next release!

BTW. The script above to find and replace fields looks scary! particularly when it breaks something šŸ˜†

jamestagal commented 1 year ago

Hi @jimafisk Since you added this feature to return nested fields in the reference widget in the last release, I just tried it out but I am still getting an undefined result in the UI. Pls check that I have set it up correctly.

https://github.com/jamestagal/plenti-educenter/blob/main/content/courses/_schema.json

Ben

jimafisk commented 1 year ago

You need _defaults.json for your courses: https://github.com/jamestagal/plenti-educenter/commit/f7fa01bfed88ed066c3c272e9c37f0123f64838a

jamestagal commented 1 year ago

Thanks @jimafisk I always forget something! šŸ˜† Actually I had content there earlier and then deleted it because I completely revamped the fields in the data source of these json files so I thought I would add it back after but forget!

Thanks again. Ben

jimafisk commented 1 year ago

No worries, I forget to add the defaults too! Plenti should do a better job of error handling this at the very least, to warn you what's happening, because as it stands it's not clear what's happening.

jamestagal commented 1 year ago

Hi @jimafisk

I don't think i should include the arrays in the _defaults.json because I am getting an 500 Internal Server Error and in the url it looks like this:

http://localhost:3000/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/course/

Pls check when you can. Ben

jamestagal commented 1 year ago

hi @jimafisk This seems to be fine now....maybe I just needed to refresh the page!

Ben

jimafisk commented 1 year ago

Ah yes that is a bug I accidentally introduced when handling 404 errors locally. I tried to fix it here: https://github.com/plentico/plenti/commit/9d36d7c7488960ec2eaff5a1f6a0d2d807ca3664

If you keep running into it, just let me know! Thanks!