Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
Keith

Dynamic Column Creation with PHP

Hello!

I used the PHP Export as a template to dynamically generate columns…
… it “sorta” works… the problem is… it is creating a unique column “set” (Import, Import #1, etc.) for every column.
Can you tell me what the issue is with my loop please?

Here is the code within my loop used to “push” a new column’s settings into the array:
( it does this 67 times in my case, so therefore is creating 67 “Import Column sets” )


$cc_ac_columns[] = array('columns' => array($row['column_name'] => array(
'type' => $row['column_type'],
'label' => $row['column_label'],
'width' => '',
'width_unit' => '%',
'field' => $row['acf_field_key'],
'character_limit' => '20',
'edit' => $column_edit_setting,
'sort' => $row['allow_sorting'],
'filter' => $column_filter_setting,
'filter_label' => '',
'name' => $row['column_name']
),),);

And here’s how I am registering it…


ac_register_columns( 'projects', $cc_ac_columns );

6 years, 7 months ago
Stefan van den Dungen Gronovius
Developer

If I understand you correctly you want to add all column to 1 column set.
In that case you have to push the column to the ‘columns’ array instead of you $cc_ac_columns arrary.
So this is what I should do

1) First build an array with all your columns -> $columns
2) Add this array to a new column set and to the ‘columns’ variable inside the array

$cc_ac_columns[]['columns'] = $columns; // $columns is the array with all your column data from the loop
ac_register_columns( 'projects', $cc_ac_columns );

I hope this will help you with your issue.

6 years, 7 months ago
Keith

Hi Stefan…

Yes, I am trying to add all columns to 1 column set. I am still running into issues. When I run the new code, Admin Columns doesn’t use my generated column set, nor does it use the set from the GUI. Instead, it defaults to Post Title, Published Date.

Here’s a bit of my code:


// this runs through a loop generating multiple columns
$cc_columns[] = array($row['column_name'] => array(
  'type' => $row['column_type'],
  'label' => $row['column_label'],
  'width' => '',
  'width_unit' => '%',
  'field' => $row['acf_field_key'],
  'character_limit' => '20',
  'edit' => $column_edit_setting,
  'sort' => $row['allow_sorting'],
  'filter' => $column_filter_setting,
  'filter_label' => '',
  'name' => $row['column_name']
),);

// then I do this:
$cc_ac_columns[]['columns'] = $cc_columns;
ac_register_columns( 'projects', $cc_ac_columns );

// when I do a print_r for $cc_ac_columns in <pre> tags, I get something like the following... which looks good yeah?

Array
(
    [0] => Array
        (
            [columns] => Array
                (
                    [0] => Array
                        (
                            [59b855e107c8b] => Array
                                (
                                    [type] => column-acf_field
                                    [label] => Job #
                                    [width] => 
                                    [width_unit] => %
                                    [field] => field_59b3f1f067fa2
                                    [character_limit] => 20
                                    [edit] => on
                                    [sort] => on
                                    [filter] => off
                                    [filter_label] => 
                                    [name] => 59b855e107c8b
                                )

                        )

                    [1] => Array
                        (
                            [59b9c23ad6a50] => Array
                                (
                                    [type] => column-acf_field
                                    [label] => ASAP?
                                    [width] => 
                                    [width_unit] => %
                                    [field] => field_59b3f4eb586f2
                                    [character_limit] => 20
                                    [edit] => on
                                    [sort] => on
                                    [filter] => off
                                    [filter_label] => 
                                    [name] => 59b9c23ad6a50
                                )

                        )

                    [2] => Array
                        (
                            [title] => Array
                                (
                                    [type] => title
                                    [label] => Job Name
                                    [width] => 
                                    [width_unit] => %
                                    [edit] => on
                                    [sort] => on
                                    [name] => title
                                )

                        )
                  )

        )

)
6 years, 7 months ago
Stefan van den Dungen Gronovius
Developer

Hi Keith,

Please have a good look at the exported example and your code, then you probably see the problem.
Your column array is not an associative array.

So $cc_columns[] = array($row['column_name'] => array(' must become something like this:
$cc_columns[ $row['column_name'] ] = array(

Plus I don’t know if you do this, but you also have to define your layout in your array (have a look at the example).
You can define your layout with something like this:

'layout'  => array(
	'id'    => 'mylayout_hash',
	'name'  => 'My Layout',
	'roles' => '',
	'users' => '',
),
6 years, 7 months ago
Keith

Nice! That did it. Thanks so much. I realize that this is *beyond* Admin Columns support, and more PHP, so I do appreciate it.

On a side note, I didn’t know what to do with the Layout code you presented, so I left it out, and things seem to be working as expected.

Anyway, go ahead and close, I am set on this one. Thanks again.

6 years, 7 months ago
Stefan van den Dungen Gronovius
Developer

Awesome and you’re welcome!
The layout ID must be unique, otherwise I think it will overwrite any other layouts with the same ID.
The Name is whatever you want as display for your layout.
With roles and users you can restrict the layout to specific roles or users. If you want to use that, you fill it in with our interface and have a look at the exported data.

I will close this ticket.

6 years, 7 months ago

You must be logged in to reply to this topic.