How to access custom fields in the category of a relationship field in expressionengine? - expressionengine

I have a custom field on my category group, called colour. I'm using a relationship field to pull the categories of the related entry into my page, but can't figure out how to get the 'colour' of the category of the related entry.
Getting the name of the category works fine by using
{exp:channel:entries channel="parent"}
{related_entry}
{related_entry:categories}
{category_name}
{/related-entry:categories}
{/related_entry}
{/exp:channel:entries}
so I would've assumed that accessing the custom field would be possible in a similar way, such as
{exp:channel:entries channel="parent"}
{related_entry}
{related_entry:categories}
{colour}
{/related-entry:categories}
{/related_entry}
{/exp:channel:entries}
but I'm having no luck. Is this possible?

Isn't it supposed to be like this? I think in your sample code, the category_name is parsed from the channel entries and not from the related_entry.
{related_entry:categories:category_name} and {related_entry:categories:colour}

Related

How to create one-to-many and many-to-many relations in Strapi v.4?

I want to create relations between Post and Tag as follows.
Post has many Tags (one-to-many)
Tags have and belongs to many Posts (many-to-many)
I created collection types for Post and Tag.
Set them accessible for public user.
I created 1 post with 3 tags.
Now, when I try to see them at http://localhost:1337/api/posts
I don’t see nested elements, i.e. tags…
Am I missing anything ?
And lastly, I was not able to create Many-to-Many relations between Post and Tag with the following error.
I understand that Tags field already exists, but I thought this is how supposed to be set.
I got reply from Strapi forum, so decided to share here.
https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest/populating-fields.html
According to the docs above, to pull (in my case) 1st level nested elements I have to add populate[0] parameter like so:
http://localhost:1337/api/posts?populate[0]=tags

Retrieving term ids for taxonomy field

I'm running into slow performance related to select N+1 issues when accessing the taxonomy terms associated with content items of a custom content type.
I've worked around issues like this in the past by getting all the related content ids up front and so I can use the ContentManager's GetMany method to get them all in one shot. For example, this has worked well for the MediaLibraryPickerField as I can easily get at the media content ids using the Ids property on the field. Here's an example similar to what I've done: Eager loading a field
I'd like to use a similar approach for getting taxonomy terms but I can't figure out how to get the term ids for a Taxonomy field on my content item. It seems that I can only access the lazy loaded term part which results in a select N+1 as they are retrieved for each content item.
Is there a way to get just the term ids for a taxonomy field without retrieving the whole term? I've spent some time digging around in the Taxonomy module source but I'm not finding any way to do it.
Any suggestions?
Try to inject IRepository<TermContentItem>. You should then be able to query over that, going beyond what the service offers natively.

Append Playa field via Safecracker

I'm building an artist directory and using Pixel & Tonic's Playa to manage each member's "fans."
In each user's profile, there is a multi-select Playa field that displays all of the members that user has "fanned" (similar to "like" on Facebook).
What I'd like to do is have a "link" that's actually a safecracker submission that will add a member's name to the user's Playa field.
Essentially, it will be a form with hidden fields for the User's ID and the screen name of the member whose page is being viewed. I want to append the screen name to the user's existing Playa field entries.
What's the best way to achieve this?
Thanks!
ty
You can use Safecracker too, and create hidden field for the Playa:
<input type="hidden" name="my_playa_field_name[selections][]" />
You must use the Playa field name here (not the field_id).
Then you can submit your Safecracker form in ajax.
You'll need to add a custom plugin to do this, so you can perform a custom SQL insert.
Not sure if you're familar with writing plugins, but you can get started by creating one via pkg.io, and then adding this function to the resulting file. Then you can create a link to a template where you place your new tag, and perhaps pass the ids for the member and the new fan via segments:
{exp:my_plugin:add_fan member_id="{segment_3}" fan_id="{segment_4}"}
This function makes the (perhaps mistaken) assumption that you're storing your members in channels (I assume so, because you're using a Playa field), and that you have access in your templates to the entry_id of both the member and fan. If not, you may need to perform some more queries to find the entry_id of each. It also calls your Playa field field_id_10 ... you'll need to replace 10 with the proper field_id of your Playa field.
function add_fan()
{
$this->EE =& get_instance();
$member_id = $this->EE->TMPL->fetch_param('member_id');
$fan_id = $this->EE->TMPL->fetch_param('fan_id');
$data = array(
'parent_entry_id' => $member_id,
'child_entry_id' => $fan_id,
'parent_field_id' => 10
);
$this->EE->db->query(
$this->EE->db->insert_string('exp_playa_relationships', $data)
);
}

In Orchard CMS, is there a way to display a list based on tags of a content type?

I created a custom content type "books" and I'm attaching tags to each item (i.e. "Featured", "New", "Children's", "Adults").
I'd like to create lists of books to display based on their tag. For example, I'd like a "Featured" list that displays all the books that have been tagged "Featured", and a "Children's Books" list that displays all the books that have been tagged "Children's".
Is there a simple way to do this (for a visual UI person who's blood pressure rises every time she has to go into the code?) :)
Taxonomies are the best way to do this. Well, tags can do that too but taxonomies are much richer.
So simplest way: yoursite/tags/featured will display all contents tagged with "featured". A little more elaborate way: taxonomies.

How to create a lookup column that targets a Doc Lib and uses the 'Name' of the document?

How do you create a lookup column to a Document Library that uses the 'Name' of the document as the lookup value?
I found a blog post that recommends adding another custom field like "FileName" and then using a item reciever to populate the custom field with the value from the Name field but that seems cheesy.
Link to the blog in case people are interested:
http://blogs.msdn.com/pranab/archive/2008/01/08/sharepoint-2007-moss-wss-issue-with-lookup-column-to-doc-lib-name-field.aspx
I've got a bunch of custom document content types that I dont want to clutter with a work around that should really work anyway.
I created a one step workflow to set the title from the name, fired on modify and created. Seems to work and took seconds to create.
One way you can do this (although not the easiest way) is by creating a custom field type that extends the SPFieldLookup class. SharePoint's field editor for Lookup fields purposefully hides any columns types that aren't supported by Lookup fields, but you can create a field editor for your custom field type that shows them.
However, I have created a Lookup column that points to a Name column in a Document Library before, and it probably doesn't work like you'd expect. While the value stored in the lookup column is valid, it doesn't show up right in List view or on the View Properties form.
The solution you posted may actually be the best way to handle this. Lookup fields require some kludges if you want to handle more complex scenarios, but that's because they're not meant to provide the same functionality as a foreign key relationship in a database.
Coding in any form always scares me. So Here's what I did: I simply renamed the Stupid "Title" Field to something else, say "Keywords", since you cant do anything with that field: cant even make it mandatory.
Then I created another Single line field called "Title" and used this field for the Lookups
Well there is a simple solution to that and in might work in some case.
In the nutshell if you make the Title field Mandatory, this will force the user to enter a title. In that manner we can use title field as a lookup field.
Now How to do that?
One you are done create a document library go to the library setting. Select Advance Setting and Select Yes for the option "Allow management of content types?".
Then go back to the Library setting and Under content types select the "Document" Content type. THen Select Title Column and then Select "Required (Must contain information)" and say OK.
Now try uploading a document to this document library. You will see Title field in the form.
Hope this helps
Cheers
Vaqar
You have to add the field as XML with the ShowField as 'FileLeafRef'
var XmlFieldDefinition = "<Field DisplayName='myLookupColumn' Type='LookupMulti' StaticName='myLookupColumn' Name='myLookupColumn' Required='FALSE' List='THE LOOKUP ID HERE' WebId='THE WEB ID HERE' UnlimitedLengthInDocumentLibrary='TRUE' Mult='TRUE' Sortable='FALSE' ShowField='FileLeafRef' />"
Field fld = fieldCollection.AddFieldAsXml(XmlFieldDefinition, true, AddFieldOptions.DefaultValue);
ClientContext.Load(fld);
ClientContext.ExecuteQuery();

Resources