Hooks
This section will introduce you to the hooks registered by this addon and how to use them.
EntryModel::newRelationshipFieldTypeLookupTableBuilder()
The new_relationship_field_type_lookup_table_builder
hook binding returns an instance of the lookup table builder.
This hook let's you override the table builder for the lookup UI.
Returns: \Anomaly\RelationshipFieldType\Table\LookupTableBuilder
Example
public function newRelationshipFieldTypeLookupTableBuilder() {
return app(\App\Example\MyLookupTable::class);
}
Automatically detected lookup tables
Lookup tables that are picked up automatically do not require you defining the hook method on your related model.
The lookup table builder location format is:
\Your\Related\Model\Namespace}\Support\RelationshipFieldType\LookupTableBuilder;
Consider the example in the Pages module:
\Anomaly\PagesModule\Page\PageModel
\Anomaly\PagesModule\Page\Support\RelationshipFieldType\LookupTableBuilder
Writing Lookup Table Builders
Writing custom option handlers is easy. Simply create your class and extend the base lookup table builder:
<?php namespace App\Example;
class LookupTableBuilder extends \Anomaly\RelationshipFieldType\Table\LookupTableBuilder
{
protected $filters = [
'title',
];
protected $columns = [
'title',
'path',
];
}
If you are not relying on automatic detection then all you need to do next is define the hook method on your related model:
public function newRelationshipFieldTypeLookupTableBuilder() {
return app(\App\Example\LookupTableBuilder::class);
}
EntryModel::newRelationshipFieldTypeValueTableBuilder()
The new_relationship_field_type_value_table_builder
hook binding returns an instance of the value table builder.
This hook let's you override the table builder for the value UI.
Returns: \Anomaly\RelationshipFieldType\Table\ValueTableBuilder
Example
public function newRelationshipFieldTypeValueTableBuilder() {
return app(\App\Example\MyValueTable::class);
}
Automatically detected value tables
Value tables that are picked up automatically do not require you defining the hook method on your related model.
The value table builder location format is:
{\Your\Related\Model\Namespace}\Support\RelationshipFieldType\ValueTableBuilder;
Consider the example in the Pages module:
\Anomaly\PagesModule\Page\PageModel
\Anomaly\PagesModule\Page\Support\RelationshipFieldType\ValueTableBuilder
Writing Value Tables Builders
Writing custom value tables is easy. Simply create your class and extend the base value table builder:
<?php namespace App\Example;
class ValueTableBuilder extends \Anomaly\RelationshipFieldType\Table\ValueTableBuilder
{
protected $filters = [
'title',
];
protected $columns = [
'title',
'path',
];
}
If you are not relying on automatic detection then all you need to do next is define the hook method on your related model:
public function newRelationshipFieldTypeValueTableBuilder() {
return app(\App\Example\ValueTableBuilder::class);
}
EntryModel::newRelationshipFieldTypeSelectedTableBuilder()
The new_relationship_field_type_selected_table_builder
hook binding returns an instance of the selected table builder.
This hook let's you override the table builder for the selected options UI.
Returns: \Anomaly\MultipleFieldType\Table\SelectedTableBuilder
Example
public function newRelationshipFieldTypeSelectedTableBuilder() {
return app(\App\Example\MySelectedTable::class);
}
Automatically detected selected tables
Selected tables that are picked up automatically do not require you defining the hook method on your related model.
The selected table builder location format is:
{\Your\Related\Model\Namespace}\Support\RelationshipFieldType\SelectedTableBuilder;
Consider the example in the Pages module:
\Anomaly\PagesModule\Page\PageModel
\Anomaly\PagesModule\Page\Support\RelationshipFieldType\SelectedTableBuilder
Writing Selected Tables Builders
Writing custom selected tables is easy. Simply create your class and extend the base selected table builder:
<?php namespace App\Example;
class SelectedTableBuilder extends \Anomaly\RelationshipFieldType\Table\SelectedTableBuilder
{
protected $filters = [
'title',
];
protected $columns = [
'title',
'path',
];
}
If you are not relying on automatic detection then all you need to do next is define the hook method on your related model:
public function newRelationshipFieldTypeSelectedTableBuilder() {
return app(\App\Example\SelectedTableBuilder::class);
}
EntryModel::getRelationshipFieldTypeOptionsHandler()
The get_relationship_field_type_options_handler
hook binding returns a handler class for setting options.
This hook let's you override the options handler for the selected options UI.
Returns: string
Example
public function getRelationshipFieldTypeOptionsHandler() {
return \App\Example\MyCustomOptions::class;
}
You can also specify the method explicitly:
public function getRelationshipFieldTypeOptionsHandler() {
return \App\Example\MyCustomOptions::class . '@method';
}
Writing Option Handlers
Similar to other option handles used in other field types, this field type uses a handle method to set the options on the passed field type instance:
<?php namespace App\Example;
class MyCustomOptions
{
/**
* Handle the options.
*
* @param RelationshipFieldType $fieldType
* @return array
*/
public function handle(RelationshipFieldType $fieldType)
{
$model = $fieldType->getRelatedModel();
$query = $model->newQuery();
$fieldType->setOptions(
$query->get()->pluck(
$fieldType->config('title_name', $model->getTitleName()),
$fieldType->config('key_name', $model->getKeyName())
)->all()
);
}
}
The handle method is called from Laravel's service container so feel free to inject other dependencies as needed.