SOLVED : Hide separator character with Display plugin

georgie

Member
Hi

I use the Display plugin to do a concatenation. Like this :

Code:
{table___field1} - {table___field2}, {table___field3}, {table___field4}.

What gives me that kind of thing :

Field1 - Field2, Field3, Field4.

Ok, it is cool ! But some field are empty. What gives me sometimes that kind of thing :

Field1 - , , Field4.

How can I fix this ?

In fact it would be enough to remove the commas if the field is empty.
Or replace two by a single comma.

Is this possible?

Thank you!
 
OK thank you, but do you have some ideas for this conditional string handling ?

Just an example, and I'll try to manage.

Thank you
 
I'm not on my PC so its hard to type

if ('{element}' != '') $s= '{element}'. ' - ';
...
return $s;

gesendet mit Tapatalk
 
OK but all this code must be in the "Default value" of the dispaly plugin (with eval) ?

Because it do not run like this...
 
It was only example code. Try
Code:
$s = '{table___field1}' . ' - ';
$as = array();
if ('{table___field2}' != '' ) $as[] = '{table___field2}';
if ('{table___field3}' != '' ) $as[] = '{table___field3}';
if ('{table___field4}' != '' ) $as[] = '{table___field4}';
 
$s .= implode(',',$as);
return $s;
 
Thank you

Well actually it works but gives me a simple concatenation.

I still have to delete a comma when two commas follow when a field is empty (,,).

And to remove the last comma when the last field is empty.

And possibly, to remove all the commas at the end of concatenation when several last fields are empty.

Not easy !

:p
 
My code is here, strictly the same like your :

Code:
$s = '{publications___type}' . ' - ';
 
$as = array();
if ('{publications___auteur}' != '' ) $as[] = '{publications___auteur}';
if ('{publications___annee}' != '' ) $as[] = '{publications___annee}';
if ('{publications___titre}' != '' ) $as[] = '{publications___titre}';
if ('{publications___titre}' != '' ) $as[] = '{publications___revue}';
if ('{publications___titre}' != '' ) $as[] = '{publications___edition}';
if ('{publications___titre}' != '' ) $as[] = '{publications___numero}';
if ('{publications___titre}' != '' ) $as[] = '{publications___pagination}';
 
$s .= implode(', ',$as);
return $s;

And here, you have an example of problem which can exist (first 2 lines) :
https://driihm.fr/productions/actions-de-valorisation

What do you think about ?
 
Ok, that's exactly what you are doing:
if titre is not empty it's pulling all other information (revue...), no matter if empty or not.
You have to do
if ('{publications___revue}' != '' ) $as[] = '{publications___revue}';
etc
 
Hi !

Thank you, it works very well indeed, but there is a problem when one of the fields contains an apostrophe (') !

Indeed it completely blocks the display of the concatenation in list view...

How can I avoid this?
 
OK, I have replace my ' by " in the code, no problem.

But what do you think about ? Is this sustainable solution?
 
Yes, I just was going to say this (will break if the string contains "...)
Not sure about the best general solution...
Maybe
$foo = $formModel->getElementData('mytable___foo');
or
$foo = $data['mytable___foo'];
will do (I never know which is working where without debugging).
 
Yup, if you run in to problems with quotes in the values of placeholders, there's really not much we can do about it, so you need to grab the data you need from the actual PHP data structures.

As Troester says, this can get a little confusing, depending where your code is running. In the case of a display element, it'll be in $data['yourtable___elementname'].

Code:
if ($data['publications___revue'] != '' )
{
    $as[] = $data['publications___revue'];
}

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top