1

I'm trying to manipulate some fields on Views with Views PHP.

I have a (term) field for author(s). It can have one author or more.

Normally it looks like as that:

Book Title | Author

My First Book | Name1 Surname1, Name2 Middle2 Surname2

My Second Book | Name3 Surname3

etc.


What I want to do is to print names as that:

Book Title | Author

My First Book | Surname1, Name1 - Surname2, Name2 Middle2

My Second Book | Surname3, Name3


In order to do that, I used Global: PHP field with Content: Authors.

Value code inside Global: PHP is that:

$names = $row->field_authors;
foreach ($names as $name) {
  $parts = explode(' ', $name);
  if (count($parts) == 1) {
    $wanted = print($name);
    continue;
  }
  $last = array_pop($parts);
  $wanted = print ($last  . implode(' ', $parts));
}
return $wanted;


Output code is:

<?php print $value; ?>


But it doesn't return anything. I tried some other similar code but nothing changed.

Is it about the Content: Authors field (multiple field settings etc.) or am I wrong about the code I used in the Global: PHP field?

  • It is better not to store PHP code in the database. You can achieve this output in the views hook. Please go through the view hooks and use the respective hooks for your output. api.drupal.org/api/views/views.api.php/group/views_hooks/7 – Vinodkumar SC Jun 28 '15 at 14:21
  • I know that an answer was already accepted, but IMHO your data is poorly designed. You should have separate fields for Name, Middle name and Surname. After fixing this you would simply need to use the field rewriting option in the view configuration, no need to use Views PHP. – user1527576 Dec 27 '15 at 3:43
1

You'll want to put that code into a view template. Go into your admin, where you can edit that View, click on Advanced, and then Theme: Information. That will give you suggestions on the file naming conventions for the row style output for your view.

But as for your PHP code, Drupal uses a lot of nested multi-dimensional arrays, so it's not as simple as $row->field_authors being an array of strings of author names (unless you're doing some preprocessing). If field_authors is a term reference, you'll need to load the taxonomy term in order to get the actual author name.

This should get you the $names array:

$names = [];
foreach($row->field_field_authors as $author_tid){
   $term = taxonomy_term_load($author_tid[0]['tid']);
   $names[] = $term->name;
}
(... the rest of your code...)
  • Thanks for your answer, could you please give more detail about how to use that? – herci Jun 29 '15 at 7:24
1

An option you can try is by using the Views Field View module. This allows you to call/render the output of another view as fields in the parent view.

Here we will be creating a View to display your authors. Here's that can generate a simple output of the authors list for a node.

  1. Create a view, for the Show setting use Content and uncheck Page and Block.
  2. Under the Advanced pane, Add a Contextual filter named Content: Nid. For the settings: (1) Select Provide default value, (2) Type would be Provide default value from URL and set Path component to 1.
  3. Under the Advanced pane again, Add a Relationship named Content: Taxonomy terms on node. For the settings: (1) Select the vocabulary you want to refer and check Require this relationship.
  4. Under the Format pane, for now we'll just use the Unfiltered List format.
  5. Under the Fields pane, assuming your taxonomy author structure has a Last Name field, add (term) Taxonomy term: Last name and (term) Taxonomy term: Name.

This will be the expected output.

enter image description here

Next we need to update the parent view. Before doing this please install the Views Field View module.

  1. Under the Fields pane, add Content: Nid. For the settings, check Exclude from display.
  2. Under the Fields pane, add Global: View. For the settings: (1) View would be the "Author taxonomy" view (2) Display can just be Master since we didn't create any View Display for the "Author taxonomy" view (3) Contextual filters would be [!nid] which is the node id that we pass into the view so it gets only the "authors" of the current node of the row.

This will be the expected output.

enter image description here

An alternative option though if you like coding is using the Views hooks. You would need to create a custom field or alter and add functionality into the multiple field view handler.

NOTE: Also wanted to stress that placing PHP code inside the database is not a best practice.

Hope this helps or give you an idea.

  • Thanks for your answer however the structure isn't the same as your assumption. There is no Last Name field. – herci Jun 29 '15 at 7:23
  • You're welcome! What is the structure? Is it just using the Taxonomy Term field Name? – Leolando Tan Jun 29 '15 at 7:43
  • Yes, there is only one Taxonomy Term field called Name. If there is first name and last name fields separately it would be easier. – herci Jun 29 '15 at 7:51
  • Ah! I see! Yeah. It would be better to manage rather than adding a custom code to always separate them or alter their order. You could use my solution even its using only the Name field thought. – Leolando Tan Jun 29 '15 at 8:06
1

Based on the answer of the question "How to print taxonomy term name with views PHP?", I could manipulate the term fields as I wanted with that code:

Output Code:

<?php
foreach($data->field_field_authors as $author) {
$term = taxonomy_term_load($author['raw']['tid']);
$name = $term->name;
$parts = explode(' ', $name);
if (count($parts) == 1) {
    print ($name);
    continue;
  }
  // get last word
  $last = array_pop($parts);
  // Print last one, comma, and rest of name
$authorManipulated = ($last .", "  . implode(' ', $parts) . " - ") ;
print $authorManipulated;
}

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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