2

So currently I am using the maxlength module (https://drupal.org/project/maxlength) in order to enforce the fields maximum length and I have a view set up that is embedded to a php Rules action (Execute custom PHP code).

Basically if a field is smaller than the maxlength defined, I am looking for a way to output the value with spaces.

For example: Maxlength 5. Field Value: xxx. Output: "xxx  " (without quotes)

I can't seem to find the right module/php code to do this.

I think I have 3 directions I can go with this.

  1. I have it save the spaces to the field based on the maxlength specified when saving the field. I don't like this option much since it actually adds spaces to the value in the database.

  2. I somehow edit the view to display the spaces based on the maxlength of the field.

  3. I edit the Rules views_embed_view php output.

Any help would be extremely helpful. Thank you.

Edit: I am using Drupal 7 by the way.

1

In template file for your field you can use good old PHP sprintf:

$right = sprintf("[%10s]\n",    $s); // right-justification with spaces
$left  = sprintf("[%-10s]\n",   $s); // left-justification with spaces

$right === "    monkey";
$left  === "monkey    ";

In your case
<?php print $output; ?>

should turn into:

 <?php printf("[%10s]", $output); /* if you want spaces before string */?>
  • So I could then create a new views-view-field tpl file for my field and alter the output. Awesome. I'm not too PHP savvy, so how would I go about using $right and 18 characters to edit the following output: <?php print $output; ?> – NWOM Aug 26 '13 at 8:23
  • @NWOM answer updated. – Mołot Aug 26 '13 at 8:28
  • Ah thank you. I realized it as well. Would it possible to pull the maximum length variable from the field settings, rather than using a static value? This would make it dynamic for all fields. Thanks alot for your help. – NWOM Aug 26 '13 at 9:50
  • @NWOM yes it would, but I can't tell how from the top of my head. Will try to come back later if I'll get an idea how. – Mołot Aug 26 '13 at 9:52
  • @NWOM if you can bring value of earlier field there, you could use printf("[%" . $length . "s]", $output); to get your result. Any string can be built dynamically. – Mołot Aug 26 '13 at 11:15

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.