0

This is a mix of a PHP and a Drupal question.

I created a custom function...

theme_field__custom_content_type($variables)

I have the DEVEL module installed and have been using it profusely. I'm trying to modify the layout/look of a custom content through a childtheme I'm developing. I'm running into an issues that is leaving me scratching my head.

I have a custom content type [netrunner_card] and it has several fields that are taxonomy references. One field in particular is [field_card_subtype]. A card can have multiple subtypes... "Location," "Seedy," "Cloud," etc. What I want to do is to take those subtypes and place them next to another field like so:

field_card_type : field_card_subtype[0] - field_card_subtype[1] - etc

So that you would see output like:

Operation : Location - Seedy

ICE : AP - Trap

etc.

What I am doing is the following... Yes, I am creating custom variables for this as it helps to collect everything in an easy to use package.

$blinksFieldName = $variables['element']['#field_name'];
switch( $blinksFieldName )
{
    case 'field_unique_card':
        $variables['card_is_unique'] = $variables['element']['#items'][0]['value'];
        break;
    case 'field_card_subtype':
        $variables['card_subtype_list'] = implode( " - ", $variables['element']['#items']);
        break;
    ...
}

When I kpr( $variables['card_subtype_list'] ) I get a string of the word 'Array'. I think that's because the breakdown of the variable in question is more like:

$variables['element']['#items'][0]['#title']

I'm very confused how you reach that level of the array when one of the placeholders is the array index... I had thought to do a for-next but I can't figure out how I would word it? Any help would be greatly appreciated.

  • Is the code you are showing for the theme_field() theme function? If that is the case, $variables should not contain $variables['element']. – kiamlaluno May 22 '13 at 22:54
1

I think I get what you're doing...you won't be able to straight implode the items array if it's a sequential set that you're using to build up a list of key/value pairs...

There are probably a few different ways to do that loop, I'd try something like this:

$keys = $values = array();
$count = 1;
foreach ($variables['element']['#items'] as $item) {
  if ($count++ % 2 == 1) {
    $keys[] = $item['#title'];
  }
  else {
    $values[] = $item['#title'];
  }
}

$output = '';
for ($i = 0; $i < count($keys); $i++) {
  $output .= $keys[$i] . ' - ' . $values[$i] . '<br />';
}
$variables['card_subtype_list'] = $output;

Hopefully someone will propose a better solution, that's not exactly elegant. Apologies if I've misunderstood.

  • the third line up from the bottom has $values[$] and I'm hitting errors. I wanted to just test your idea and see if it helped me find this 'better solution' of which you speak... but I keep getting errors. Any suggestions? – randomblink May 22 '13 at 19:21
  • 1
    It was a typo sorry, should have been $values[$i]. That probably won't do the whole thing, but the logic to get the items into key/value pairs is there – Clive May 22 '13 at 19:31
  • 1
    I don't get how that loop can obtain $variables['element']['#items'][$index]['#title'] values. – kiamlaluno May 22 '13 at 22:31
  • Ok... a couple things @Clive. #1) I was getting an Undefined index #title. So I KPR'd and see that $variables['element']['#title'] doesn't exist. I get a tid (String) and a taxonomy_term (Object), but that's all. So I dug around and I see that THIS get's to what I am looking for... $variables['element'][0]['#title'] – randomblink May 23 '13 at 13:01
  • Second thing @Clive. What does the following line do: $count++ % 2 == 1 My php skills are rusty (at best)... – randomblink May 23 '13 at 13:01
0

What I did to make this work is as follows. Inside template.php, in the following function:

function blinks_responsive_field__netrunner_card($variables)

I checked the $delta in the foreach for which subtype I was on.

$subTypeSpacer = '';
if( $blinksFieldName == 'field_card_subtype' ){ if( $delta > 0 ){ $subTypeSpacer = ' - '; } }

So if we were on subtype #0 (the first one) then don't add the hyphen. Otherwise add it. Then to insert the subTypeSpacer into our output I did the following:

$output .= '<div class="' . $classes . '"' . $variables['item_attributes'][$delta] . '>' . $subTypeSpacer . drupal_render($item) . '</div>';

This put everything together in our $variables. Now we have to call them. So we move ahead to node--netrunner-card--Operation--full.tpl.php

In here, we are custom styling the output for an Operation card. Jump down to where render( $content ) used to go, and we see this:

<div class="field-card-type"><?php print render( $content['field_card_type']); ?> : <?php print render( $content['field_card_subtype'] ); ?></div>

So, now we have all of the elements together, but they sure don't look pretty. These divs are treated like block elements, so they each get their own line. Not acceptable. So let's jump to style.css and do this...

div.field-card-type div
{
    display: inline;
}

Done.

If you'd like to see a sample here is what it looks like: A single operation card with the card subtypes styled like desired.

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.