Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default column populate method? #67

Open
iwillhappy1314 opened this issue Oct 9, 2020 · 2 comments
Open

Add default column populate method? #67

iwillhappy1314 opened this issue Oct 9, 2020 · 2 comments

Comments

@iwillhappy1314
Copy link

In my usecase, i added serval post metas in columns, is there any way to set default column polulate method? so we can keep the code more dry?

maybe add a method populateDefault like the code below?

$client = new PostType('client');

$client->columns()
     ->hide([
         'title',
         'date',
     ])
     ->add([
         '_name'      => __('Name'),
         '_deal_date' => __('Deal time'),
        '_is_dealed'     => __('Price'),
         '_age'       => __('Age'),
         '_phone'     => __('Phone'),
         '_price'     => __('Price'),
     ])
    ->populateDefault(function ($column, $post_id)
    {
        echo get_post_meta($post_id, $column, true);
    })
   ->populate('_is_dealed', function ($column, $post_id)
     {
         $deal_price = get_post_meta($post_id, 'deal_price');

         echo ($deal_price) ? '<span class="is-success">Dealed</span>' : '<span class="is-default">not dealed</span>';
     });
@dweipert-3138720606
Copy link

dweipert-3138720606 commented Dec 17, 2020

Or instead have the populate method accept an array of column keys as well.

public function populate($columns, $callback)
{
    $columns = (array)$columns;
    foreach ($columns as $column) {
        $this->populate[$column] = $callback;
    }

    return $this;
}

@jjgrainger
Copy link
Owner

Thanks both, the populate method accepting an array of column keys seems the simplest and will look to implement this in the future.

It should also be possible to define a named function and pass this to the populate method to keep things more DRY.

<?php
// Create an array of columns to add.
$columns = [
    '_name'      => __('Name'),
    '_deal_date' => __('Deal time'),
    '_is_dealed' => __('Price'),
    '_age'       => __('Age'),
    '_phone'     => __('Phone'),
    '_price'     => __('Price'),
];

// Define a default populate column callback.
function populate_column( $column, $post_id) {
    echo get_post_meta($post_id, $column, true);
}

// Create the post type.
$client = new PostType( 'client' );

// Modify columns.
$client->columns()
    ->hide([ 'title', 'date' ])
    ->add($columns);

// Loop over all added columns and set the default populate column callback.
foreach ( $columns as $key )  {
    $client->columns->populate( $key, 'populate_column' );
}

// Populate is dealed column differently.
$client->columns()->populate( '_is_dealed', function ( $column, $post_id ) {
    $deal_price = get_post_meta( $post_id, 'deal_price' );

    echo ( $deal_price ) ? '<span class="is-success">Dealed</span>' : '<span class="is-default">not dealed</span>';
} );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants