1

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:

The roles

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:

The two field collections

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):

  1. 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)
  2. 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?
  3. 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.

9
  • I am more of a visual person. If you can draw a picture/diagram of what you are trying to achieve will go a long way for me, and perhaps for others, thx.
    – No Sssweat
    Commented Feb 4, 2016 at 9:16
  • @NoSssweat There isn't really so much of a picture to be drawn here. I've added a visual representation of how the field collections are set up if that helps.
    – UltraBob
    Commented Feb 4, 2016 at 9:23
  • That actually helps. Is Role Name 1 the taxomy term name (from term reference) or the referenced user role (from entity reference)?
    – No Sssweat
    Commented Feb 4, 2016 at 9:29
  • 1
    If you need help with views and if you want, I can help you with team viewer it's free for personal use.
    – No Sssweat
    Commented Feb 6, 2016 at 13:12
  • 1
    you can embed your view on your template with php like this <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)
    – No Sssweat
    Commented Feb 6, 2016 at 13:14

1 Answer 1

1

The Drupal Way

Add a field of Entity Reference, Widget: Auto Complete Tag Style and change the number of values from 1 to unlimited.

  • Select User as your target type

enter image description here

After you click save field settings you are taken to another screen, scroll down and find Number of values and change it to Unlimited and Save Changes.

enter image description here

All you gotta do is create an entity reference field for each. Authors, Editors etc...

On your entity reference field since we are using Autocomplete (tag style) you start to type a user name and select the user. Then add a comma , to add more users

Ex: enter image description here


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

Views

With the views module you can use this to create your display.

Uncheckmark view page and checkmark View BLOCK

Create a new view of content [your content type]. Type: Fields Format: Unformated List

In FIELDS add Editors and Authors fields.

Lastly,

Under Advanced, Add a CONTEXTUAL FILTER of nid >> select default value, then in drop down select nid from url. (Note: the preview will no longer work, since the preview url does not have a nid)

Your view block will be listed in BLOCKS, but you can also use Panels to insert your view block. You need to enable a module for that. I forgot which one. Sorry can't check right now.


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.

You can use Metatag module for this.

The Metatag module allows you to automatically provide structured metadata, aka "meta tags", about a website. In the context of search engine optimization, when people refer to meta tags they are usually referring to the meta description tag and the meta keywords tag that may help improve the rankings and display of a site in search engine results.

Video Tutorial

11
  • I think you are missing that I need both users and non-users to be able to be assigned to each role.
    – UltraBob
    Commented Feb 4, 2016 at 10:33
  • right, you just create a text field for those non users. (ex: lets call them Editors Text) Add the field to your view. Click hide from display. Make sure this field is above the entity reference field. Then click on the Editors entity ref field and rewrite the results. Use the replacement patters. [field_editors] <span class="test">[editors_text]<span> then with css you can add a coma at the beginign of the editors_text list.
    – No Sssweat
    Commented Feb 4, 2016 at 10:46
  • rewrite results example drupal.stackexchange.com/questions/188892/…
    – No Sssweat
    Commented Feb 4, 2016 at 10:49
  • In actuality there are 5 or 6 roles, not just the 2. I need to think about whether that will make a good entry experience, and whether I can make the migration work that way. I do like the simplicity of the approach on the output side. One other downside to this approach is that I was intending to add an organization field for the other users collection to allow the specification of affiliation for non-staff contributors.
    – UltraBob
    Commented Feb 4, 2016 at 10:50
  • @UltraBob another approach is that you can create dummy user accounts for non-staff members. Perhaps you can create an non-staff role give them a non-staff role. You can add users at /admin/people/users (I hope this link is correct, just going off memory here)
    – No Sssweat
    Commented Feb 4, 2016 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.