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

Feature: Add support for Table field #61

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/Field/Table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Corcel\Acf\Field;

use Corcel\Acf\FieldInterface;

/**
* Class Text.
*
* @author Junior Grossi <juniorgro@gmail.com>
*/
class Table extends BasicField implements FieldInterface
{
/**
* @var string
*/
protected $value;

/**
* @param string $field
*/
public function process($field)
{
$this->value = $this->fetchValue($field);
}

/**
* @return string
*/
public function get()
{
return $this->format_value($this->value);
}

protected function format_value($value)
{
$a = json_decode($value, true);

$value = false;

// IF BODY DATA

if (count($a['b']) > 0) {
// IF HEADER DATA

if ($a['p']['o']['uh'] === 1) {
$value['header'] = $a['h'];
} else {
$value['header'] = false;
}

// BODY

$value['body'] = $a['b'];

// IF SINGLE EMPTY CELL, THEN DO NOT RETURN TABLE DATA

if (
count($a['b']) === 1
and count($a['b'][0]) === 1
and trim($a['b'][0][0]['c']) === ''
) {
$value = false;
}
}

return $value;
}
}
5 changes: 4 additions & 1 deletion src/FieldFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Corcel\Acf\Field\Term;
use Corcel\Acf\Field\Text;
use Corcel\Acf\Field\User;
use Corcel\Acf\Field\Table;
use Corcel\Model;
use Illuminate\Support\Collection;

Expand Down Expand Up @@ -49,7 +50,6 @@ public static function make($name, Model $post, $type = null)
$type = $fakeText->fetchFieldType($key);
}


switch ($type) {
case 'text':
case 'textarea':
Expand Down Expand Up @@ -108,6 +108,9 @@ public static function make($name, Model $post, $type = null)
case 'flexible_content':
$field = new FlexibleContent($post);
break;
case 'table':
$field = new Table($post);
break;
default: return null;
}

Expand Down