I am wondering if it is possible to directly edit values of a has_one relationship in SilverStripe.
For example:
Person (extension of SiteTree) has one Job (extension of DataObject) where Job has the db fields of title, salary, location etc.
Is it possible in the CMS to place a form in a new tab to edit the values of Job form the Person page? So all of the form fields from the Job DataObject are placed on the Person page and will save to the Job table when I hit save on the Person page?
I can get this to work with inline gridfield editing (thanks to the editable columns class in gridfieldextensions) but I believe this requires a has_many / many_many relationship? It also is not suitable for adding images and managing objects that have a large number of fields.
I hope that make sense. Let me know if you need more clarification.
EDIT: I found this hasonefield module which is very out of date but does 80% of what I am after, it would be amazing if it didn't take you off the page though and you could edit the fields within the parent page (Person).
there is a fork of simon's orginal has-one-edit module that is still available and composer installable, which does the logic for editing fields (or all fields) of a has_one relation. Install it running
composer require stevie-mayhew/hasoneedit:1.0.x#stable
Then you can define several fields of your has one relation, naming should be HasOneName-_1_-FieldName.
From the docs:
For example, say you have a has_one called Show and that has_one has a field called Title you want to edit. You'd add the field TextField::create('Show-_1_-Title', 'Show Title').
To add support to your own forms, you need to add the sgn_hasoneedit_UpdateFormExtension extension to your controller and call $this->extend('updateEditForm', $form) before returning the form to the template. Without this, the fields will not get populated with the values from the has_one though saving will work.
Related
Scenario: I have to create an offer for customers. Each offer contains a list of items referring to products in the database. Those products may have different variants / options that affect pricing (which is calculated dynamically on change - no problem here). In my form, there is an autocomplete for product names which adds items assigned to the selected product to the collection.
Problem: When working with collections, symfonys way to handle adding items are prototypes. Unfortunately this process is handled completely on client side and thus there is no context given that could trigger a form event to add the product-dependent fields.
I have a few solutions that could solve this problem, but I wonder if there is kind of a best practice for this issue?
Solution 1: Autosubmit form after selecting a product
This is the simplest solution since after creating a new item with a product on server-side I have no issues working with form events and could add fields as needed. However the form would reload everytime, which is not so good from an UX point of view.
Solution 2: Don't use prototype and create form for an unpersistent dummy-entity on serverside.
This would be a little more complex. When adding a product, a server side script would create an item with that product assigned and create a form for that. There are a few issues with this e.g. regarding form ids / field names I think. This is currently my prefered solution.
Solution 3: Rendering all fields possible and dynamically hide them if not needed (impossible)
This is not possible as the fields are too dynamic for this. There is even a one-to-many association between the items and further options that need to be rendered. So knowing the product context is a must.
Resources:
An 8 year old github issue for this topic
I have a form on a node, that displays 2 fields that are entity reference fields from the current node page. It shows the correct info on the label of the fields, but as soon as you save the form, it saves the nodes title and not the correct info.
See screenshot at https://www.dropbox.com/s/rwj1lu1d34zgb53/ScreenshotEntityform.jpg
I don't know PHP or how to mod/write a Drupal module, I just need some guidance as to if this is possible and how to do it, so any help would be really really great, thanks.
There is a setting to edit how an entity reference field is displayed.
If you go to Structure > Content Types > Your Content Type > Manage Display then you will see a select box to control the format of each field.
The entity reference field will let you choose between either label, entity ID or rendered entity. It sounds like at the moment it is set to label so is showing the node title. If you set it to show the rendered entity then it will show all fields that belong to the referenced entity.
The easiest way to hide fields you don't want displayed would be to install the Field Permissions module. This will give you a setting for each field that allows you to choose who can see it. If you don't want to use the Field Permissions module you could create a custom template but it sounds like your trying to avoid that...
Say I have 2 content types "Artist" and "Biography". I can use Entity Reference to link one Biography with one Artist (it's a one to one relationship).
Now when creating a new Artist, can I have a tab or a link to add an artist biography directly, instead of having to create the artist first, then the biography, and link it with an artist?
Thank you.
I don't know of one for Entity Reference but if you can swap that out in favour of the References module you can use References dialog which would be suited to your use case:
This module extends reference fields like the user and node reference fields by adding links to add, edit and search for references through a dialog. This allows for a workflow where the user can create all the references to a node at the same time as she is creating it, a process which sometimes get's a bit backwards, when a user for instance needs to create all the facts for an article.
If you're changing to a different relationship module, there's also the Inline Entity Form module which:
Provides a widget for inline management (creation, modification, removal) of referenced entities.
It embeds the add/edit form for the referenced entities into the primary entity's form so you can create multiple related entities from the same page. It also has support for nodes built right in.
So if I have 2 custom content types book and review, I'd like a user(when viewing a specific book) to be able to create/attach a review to the book.
I've tried a few options.
1) Created a "create review" formblock and using panels made a custom node view panel for review, but I couldn't get the relationship to work.
2) I also tried overriding comments on the book type with the node type review, the problem with this is I'd like users to be also be able to comment on the book and I couldn't see the ability to add multiple comment types.
This seems pretty easy to do on node edit with node reference extension so I'm hoping this isn't a big ask.
You need nodereference and nodereference_url modules.
You can simply create a nodereference field in your review content type, and configure it with nodereference_url widget.
You partially answered your own question. With the nodereference module in D6 (or just references in D7), you can create a link in the same place 'add a comment' appears (the .links div) which says "add a review". This link is because "reviews" has a field called something like "book reference" which is a noderef field. This review will automatically fill in the field with the book where the user clicked the link and, depending on how you set the field up, send you back to the referenced node (book) or the new review.
You can find an interesting screencast there that show how to use Views Attach and Node Reference URL to do what you want :
http://mustardseedmedia.com/podcast/episode37
I have a parent/child relationship (based on the cck node reference field).
Recipe Group contains a node reference to the type Recipe. It's a 1 to many relationship.
What I need to do is create a view that displays the recipe group information, and then under it, the recipes that have been assigned to it's cck field.
So
RecipeGroup1 - Title
Recipe1, Recipe2, Recipe3
RecipeGroup2 - Title
Recipe4, Recipe5, Recipe6
etc etc
I've created the view to pull the list of recipe groups.. but I have no idea how to retrieve the recipes that belong to the group?
I've tried googling, but we are on a tight time line and would appreciate any assistance.
Thanks.
Take a look at http://drupal.org/project/views_field_view. It lets you combine two views (groups, recipes), inserting the recipes view as a field into the groups view. Using this, you can have as many fields as you want for the group.
You can use the Views attach module to associate a view with a specific content type and display that view on that content type's node page.
There is a webcast here that demonstrates how the module can solve a problem similar to yours.
However, the webcast assumes your nodereference CCK field is on the child content type and is pointing to the parent content type, not the other way around like you have it.
Instead of using the nid of the recipe group as an argument to the view (as shown in the video) you may be able to use one of the recipe group's tokens (the nodereference field referencing its children). You can see that option at 9:32 in the video.
Whatever you decide to do, I think Views attach will likely be your answer.
Include the cck reference node field as an output field to your view. After, you can 'rewrite the output of this field'.. so that it looks however you want.
add noderefernce field in to views fileds
select check box to group field result in node ref field settings
theme that field as you want