-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for new Member fieldtype
- Loading branch information
1 parent
166e39a
commit 1df1247
Showing
9 changed files
with
189 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Expressionengine\Coilpack\Fieldtypes; | ||
|
||
use Expressionengine\Coilpack\Contracts\Field; | ||
use Expressionengine\Coilpack\Contracts\ListsGraphType; | ||
use Expressionengine\Coilpack\FieldtypeOutput; | ||
use Expressionengine\Coilpack\Fieldtypes\Presenters\MemberPresenter; | ||
use Expressionengine\Coilpack\Models\FieldContent; | ||
|
||
class Member extends Fieldtype implements ListsGraphType | ||
{ | ||
protected $presenter; | ||
|
||
public function __construct(string $name, $id = null) | ||
{ | ||
parent::__construct($name, $id); | ||
$this->presenter = new MemberPresenter; | ||
} | ||
|
||
public function apply(FieldContent $content, $parameters = []) | ||
{ | ||
$data = $this->presenter->present($content, $parameters); | ||
|
||
return FieldtypeOutput::for($this)->value($data); | ||
} | ||
|
||
public function parametersForField(Field $field = null): array | ||
{ | ||
return $this->presenter->defineParameters(); | ||
} | ||
|
||
public function graphType() | ||
{ | ||
return 'Member'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Expressionengine\Coilpack\Fieldtypes\Presenters; | ||
|
||
use Expressionengine\Coilpack\Models\FieldContent; | ||
use Expressionengine\Coilpack\Models\Member\Member; | ||
use Expressionengine\Coilpack\Traits\HasArgumentsAndParameters; | ||
|
||
class MemberPresenter extends Presenter | ||
{ | ||
use HasArgumentsAndParameters, Traits\QueriesRelationships; | ||
|
||
public function present(FieldContent $content, $arguments) | ||
{ | ||
$query = $this->buildRelationshipQuery($content, new Member); | ||
|
||
return $query->get(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Expressionengine\Coilpack\Fieldtypes\Presenters\Traits; | ||
|
||
use Expressionengine\Coilpack\Model; | ||
use Expressionengine\Coilpack\Models\FieldContent; | ||
use Illuminate\Support\Str; | ||
|
||
trait QueriesRelationships | ||
{ | ||
public function buildRelationshipQuery(FieldContent $content, Model $model, $tableName = null) | ||
{ | ||
$isGrid = $content->field->field_type === 'grid'; | ||
$isFluid = $content->hasAttribute('fluid_field'); | ||
$fluidFieldId = ($isFluid) ? $content->fluid_field_data_id : 0; | ||
|
||
$query = $model::query(); | ||
$tableName = $tableName ?: Str::singular($model->getTable()).'_relationships'; | ||
|
||
if ($content->entry->isPreview()) { | ||
$query->whereIn('member_id', $content->data['data'] ?? [0]); | ||
} else { | ||
$query->select("{$model->getTable()}.*") | ||
->join($tableName, $model->getKeyName(), '=', 'child_id') | ||
->when($isFluid, function ($query) use ($fluidFieldId, $tableName) { | ||
$query->where("$tableName.fluid_field_data_id", $fluidFieldId); | ||
}, function ($query) use ($tableName) { | ||
$query->where("$tableName.fluid_field_data_id", 0); | ||
}) | ||
->when($isGrid, function ($query) use ($content, $tableName) { | ||
$query->where("$tableName.grid_field_id", $content->field->field_id) | ||
->where("$tableName.grid_row_id", $content->grid_row_id) | ||
->where("$tableName.grid_col_id", $content->grid_col_id); | ||
}, function ($query) use ($content, $tableName) { | ||
$query->where("$tableName.field_id", $content->field->field_id) | ||
->where("$tableName.grid_field_id", 0); | ||
}) | ||
->where("$tableName.parent_id", $content->entry_id) | ||
->orderBy('order'); | ||
} | ||
|
||
return $query; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Tests\Fieldtype; | ||
|
||
use Expressionengine\Coilpack\Models\Channel\ChannelEntry; | ||
use Tests\TestCase; | ||
|
||
class MemberTest extends TestCase | ||
{ | ||
public function test_member() | ||
{ | ||
$entry = ChannelEntry::where('title', 'Test Fieldtypes')->first(); | ||
|
||
$output = $entry->test_members->value(); | ||
|
||
$this->assertEquals('admin', $output->toArray()[0]['username']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.