jmartsch

[SOLVED] Add class to FieldtypeTable before outputting it

Recommended Posts

How do I add a class "uk-table" on the frontend to a FieldTypeTable? I am iterating through all elements from an admin template and eventually modify the values (for example concatenate the values of a Page field with a comma), but I don't know how to add the class to the table.

The code that I am using right now is in a gist at https://gist.github.com/jmartsch/768d9dc19293528fe9ae82b7a8ee1c42#file-home-php

In line 15 https://gist.github.com/jmartsch/768d9dc19293528fe9ae82b7a8ee1c42#file-home-php-L15 I tried to hook into the rendering, but as it is no Inputfield anymore, the hook does not work.

Can someone help me please?

Share this post


Link to post
Share on other sites

I found a way with str_replace, but I think this is not the best way.

Here is what I added to my getFieldValue function:

if ($fieldType == 'FieldtypeTable') {
        $value = str_replace('ft-table', 'uk-table', $value);
        return $value;
      }

Suggestions, how I can do it better?

Share this post


Link to post
Share on other sites

$inputfield->addClass() does not work (you already have this line in the linked gist)? Or did you already solve it like this?

Share this post


Link to post
Share on other sites

Sadly $inputfield->addClass()  does not work.

Share this post


Link to post
Share on other sites

Try this:

$inputfield->class .= ' your-classes';// @note the space

 

Share this post


Link to post
Share on other sites

@kongondo This also doesn't work.

@OLSA This won't work because it is a FieldtypeTable and not a FieldtypePageTable, as you can see in the screenshot:

image.png.3a5e2ee6f36f6905fb350b87d76e3ede.png

Share this post


Link to post
Share on other sites

Your screenshot shows an InputfieldTable, which is neither a FieldtypeTable nor a FieldtypePageTable 😉 

Share this post


Link to post
Share on other sites

If you render the value of the field you get the table rendered by class TableRows. It's nothing to do with the Fieldtype or Inputfield render.

So you can simply use the render function of the value == TableRows object and add your options.

$value = $page->mytable->render(array("tableClass" => "uk-table"));

Though nothing wrong with you str_replace. I don't get how that is bad but anyway.

  • Like 4

Share this post


Link to post
Share on other sites

Thanks @Soma, this works fine. Maybe it is a solution for other fieldtypes I have too.

Share this post


Link to post
Share on other sites
3 minutes ago, jmartsch said:

Thanks @Soma, this works fine. Maybe it is a solution for other fieldtypes I have too.

I don't know of any other Fieldtypes that work this way.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.