I have a multi-value double field (text field/text field).

for styling purpose I need the correct snippet of code that gets the character's count of the field which has the max length among the rest.

and depending on this value i can choose which css class is suitable for the whole multiple fields.

I mean I want to build my style upon the maximum length, so it will not break.
like if max length < 30 Chars , then select (short text class) <== taxonomy term
like if max length between 31-50 Chars , then select (medium text class)
like if max length > 50 Chars , then select (long text class)

With little php knowledge. but can I improve it. and with using this combination of two modules which can save a lot of time and effort :
1- Field Formatter CSS Class can change the css class for this field ( by selecting a taxonomy term which represent a css class)
2- CSS field formatters which can accept php snippets and can programmatically modify the CSS class term field (mention above #1)

- snippets are preferred , but other solutions are very welcome.

Comments

theroyal created an issue. See original summary.

Chi’s picture

Status: Active » Needs review

That's fairly simple. You can override theme function and calculate field length there.

/**
 * Implements theme_double_field().
 */
function YOURTHEME_double_field($vars) {
  $element = $vars['element'];
  $settings = $element['#display']['settings'];

  if ($settings['style'] == 'link') {
    $output = l($element['#item']['first'], $element['#item']['second']);
  }
  elseif ($settings['style'] == 'simple') {
    $output = $element['#item']['first'] . $element['#item']['second'];
  }
  else {
    $class = $settings['style'] == 'block' ? 'clearfix' : 'container-inline';
    $output = '<div class="' . $class . '">';
    $length = drupal_strlen($element['#item']['first']) > 50 ? 'long' : 'short';
    $output .= '<div class="double-field-first text-' . $length . '">' . $element['#item']['first'] . '</div>';
    $length = drupal_strlen($element['#item']['second']) > 50 ? 'long' : 'short';
    $output .= '<div class="double-field-second text-' . $length . '">' . $element['#item']['second'] . '</div>';
    $output .= '</div>';
  }
  return $output;
}
theroyal’s picture

is this snippet ready to use in
1- CSS Field formatters module ?
OR
2- I need to add it to the theme files ?

Chi’s picture

It should be placed to template.php file of your theme.

theroyal’s picture

I really appreciate your help, and what i did is :

1- removed the (php) ? at the start and end.
2- replaced YOURTHEME with the theme name.
3- I added the code at the end of template

i have few questions:
1- Am I correct in the above steps? sorry its almost my 1st time to deal with code in drupal.
2- do I have to modify any other values in the code?
3- do I have to add a certain class in my style.css file? I mean did you add one or more in the code as an example?
4- I understand that this code will let me to avoid using the modules I mentioned in the issue. am I right?
5- does it count each subfield (this what is needed),
or the sum of sub1st+sub2nd (this is not what i mean)?

Chi’s picture

The function should be placed to your template.php file. It counts values of each subfield and provides CSS classes (text-long, text-short).

theroyal’s picture

ok got it.
am so sorry for bothering

1- I want if the value is (long) I need to apply 2 classes not one:
a- subfield1-long-text (for all subfield1) so i can allign left for example.
b- subfield2-long-text (for all subfield2) so i can allign right for example.

2- what about if I want to have more selections like :
0-25 very-short
26-40 short
41-55 miduim
56-70 long
70 < very long

theroyal’s picture

after re-look to the code, i noticed the following :

1- you are trating the subfield1 seperately than subfield2.
the needed is : check in both subfield1 and subfield2, find the any field which has the highest value in length.
lets say :
subfield1 | subfield2
20 | 15
31 | 34
11 | 99

so the value is (99)
then the we will set:
for subfield1: subfield1-very-long-class
for subfield2: subfield2-very-long-class

thats it

2- you are providing only 2 options long and short, while i need more options as mentioned in the earlier comment.

Chi’s picture

It's just an example of how it could be done. The code is very simple. You can modify it as you wish even you have never worked with PHP.

// Length of the first value.
$length = drupal_strlen($element['#item']['first']);

if ($length < 25)  {
  $class = 'very-short';
}
elseif ($length < 40) {
  $class = 'short';
}
else {
  $class = 'long';
}
theroyal’s picture

yes the code looks great and simple
and I really appreciate your help.

but am afraid that the most important issue -which is doublefield related- is not clear yet,
maybe i didnt explain it very well.

I want to check all values and for both subfields,
example:
if i have 10 multivalues and each value has 2 subfields, then we have 20 subfields values here.
so the wanted is the highest length among these 20 subfields.
thats it.

maybe we need a loop here ?
if it needs a loop, then how to implement it with doublefield.

Chi’s picture

Loop does not help in this function because it takes only on field value. You have to implement custom field formatter which is much more complected or use javascript.

theroyal’s picture

ok what about if I need it to set the css class only when this field is filled or updated.
what is the recommended way ?

Chi’s picture

I don't even know. Some custom module perhaps.

theroyal’s picture

ok thanks for your valuable tips.
really it was a great help.

Chi’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.