Skip to content

Commit

Permalink
Merge pull request #124 from moisespsena/form_element_select_add_load…
Browse files Browse the repository at this point in the history
…_options_query_preparer

Add callback for prepare load options Query.
  • Loading branch information
butschster committed May 31, 2016
2 parents 3eecadd + ccd9815 commit 3ffc03f
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/Form/Element/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class Select extends NamedFormElement
*/
protected $fetchColumns = [];

/**
* @var function|\Closure|object callable
*/
protected $loadOptionsQueryPreparer;

/**
* @param string $path
* @param string|null $label
Expand Down Expand Up @@ -251,6 +256,41 @@ public function getFetchColumns()
return $this->fetchColumns;
}

/**
* Set Callback for prepare load options Query.
*
* Example:
* <code>
* <?php
* AdminFormElement::select('column', 'Label')
* ->modelForOptions(MyModel::class)
* ->setLoadOptionsQueryPreparer(function($item, QueryBuilder $query) {
* return $query
* ->where('column', 'value')
* ->were('owner_id', Auth::user()->id)
* });
* ?>
* </code>
*
* @param callable $callback The Callback with $item and $options args.
* @return $this
*/
public function setLoadOptionsQueryPreparer($callback)
{
$this->loadOptionsQueryPreparer = $callback;

return $this;
}

/**
* Get Callback for prepare load options Query.
* @return callable
*/
public function getLoadOptionsQueryPreparer()
{
return $this->loadOptionsQueryPreparer;
}

/**
* @param array $keys
*
Expand Down Expand Up @@ -342,6 +382,11 @@ protected function loadOptions()
$options->select($columns);
}

// call the pre load options query preparer if has be set
if (! is_null($preparer = $this->getLoadOptionsQueryPreparer())) {
$options = $preparer($this, $options);
}

$options = $options->get();

if (is_callable($this->getDisplay())) {
Expand Down

0 comments on commit 3ffc03f

Please sign in to comment.