Note: I've not accepted an answer on this because the answer I got wasn't what I needed. In the end I abandoned field collections in favor of paragraphs for this task.
Update: I really appreciate @NoSssweat's answer, but it isn't what I'm looking for in this case, so I'm still looking for an answer. Please help me better understand how to crawl through and combine two field collections.
Update 2: I found a way to build a view that would do roughly what I want with what I have as long as I don't combine the staff and other fields. Would there be a way to incorporate the view into my template, or perhaps it would be better to do the whole page as a view and attach the field collection views. I assume that I could handle moving the attachment into the appropriate place through some views preprocess function?
Update 3: I've just found out one more requirement: the order of the contributors is important, and it could be any mix of staff and non staff contributors (staff first, non-staff first, a mix). I think I need to have a look at the Paragraphs module.
This is a more complex version of How can I combine the output of two separate fields in template.php?.
I have two field collections. One consists of a term reference for role:
and a entity reference for a user. The other consists of the same term reference and a text field to enter the names of contributors who are not users:
I want to sort all of those values into different roles and render them out as such:
Role Name 1: rendered Staff link, rendered staff link, external contributor
Role Name 2: rendered staff link, external contributor, external contributor
Role Name 3: external contributor, external contributor
(This is sorting all the people into roles as assigned, staff members first)
Here is how a page might actually appear (imagine names that are staff members are underlined links to user profiles, no other links exist):
Editor: Doe, John; Doe, Jane; Yamada, Tarou; and Yamada, Tomoko. Author: Yamada, Tarou and Doe, Betty
I have been trying to get something going using mytheme_field__field_collection_name()
, but am running into a few stumbling blocks.
Here is pseudo code for how I imagine this should work:
<?php
function mytheme_field__field_collection_for_staff($variables) {
isset($some_persistent_variable_roles) !! $some_persistent_variable = array();
foreach ($variables['items'] as $item) {
$role = grab the safe to print role name;
$staff_member = grab the safe to print Staff Name link
if (!in_array($role, $some_persistent_variable_roles) {
$some_persistent_variable_roles[] = $role;
}
$some_persistent_variable_roles[$role][] = $staff_member
}
}
function mytheme_field__field_collection_for_others($variables) {
isset($some_persistent_variable_roles) !! $some_persistent_variable = array();
foreach ($variables['items'] as $item) {
$role = grab the safe to print role name;
$person = grab the safe to print name text field
if (!in_array($role, $some_persistent_variable_roles) {
$some_persistent_variable_roles[] = $role;
}
$some_persistent_variable_roles[$role][] = $person
}
}
I would then iterate through each of the roles, printing out a semicolon separated list of the staff members (a link to their profile with text of FamilyNameField, FirstNameField), and then the other contributors (plain text names)
So I have a few issues here (they may be the wrong questions, please educate me):
- How can I persist a variable across multiple field processing functions so that I can later output them as part of the node. (I tried
variable_set
, but that seems to persist beyond just the page load which is not really what I want, I just want to build up values between function calls) - These two functions normally return some kind of output so would it make sense to return null with the first one and the rendered code with the second?
- Or is there some other way that this calculated value should be injected back into the template?
Drupal is pretty new for me, so please tell me if I'm barking up the wrong tree with how I'm approaching this, I'm not married to this approach, I'm trying to learn the drupal way of doing it so I won't be fighting drupal at every turn.
Additionally: Later on I'll need to use these values, possibly in the same form, possibly not, to populate metadata tags, so that google scholar and the like can find and properly cite the work.
Role Name 1
the taxomy term name (from term reference) or the referenced user role (from entity reference)?<php print views_embed_view('my_view', 'block_1', $arg1, $arg2); ?>
replace my_view with your view name, replace block_1 with your machine name. If you want to pass an argument, replace $arg1 with your value. (contextual filter argument)