diff --git a/src/Field/Table.php b/src/Field/Table.php new file mode 100644 index 0000000..27215c8 --- /dev/null +++ b/src/Field/Table.php @@ -0,0 +1,69 @@ + + */ +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; + } +} diff --git a/src/FieldFactory.php b/src/FieldFactory.php index 2709458..15f732a 100644 --- a/src/FieldFactory.php +++ b/src/FieldFactory.php @@ -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; @@ -49,7 +50,6 @@ public static function make($name, Model $post, $type = null) $type = $fakeText->fetchFieldType($key); } - switch ($type) { case 'text': case 'textarea': @@ -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; }