Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
colintucker committed Jan 23, 2018
2 parents 5cf2b96 + 6ddd379 commit 3e5a33a
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/Forms/Select2AjaxField.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,27 @@ public function getFormatSelection()
return $this->formatSelection;
}

/**
* Updates the text field, search fields and sort order to the specified field name.
*
* @param string $field
* @param string $order
*
* @return $this
*/
public function setDescriptor($field, $order = 'ASC')
{
// Define Attributes:

$this->setTextField($field);
$this->setSearchFields([$field]);
$this->setSortBy([$field => $order]);

// Answer Self:

return $this;
}

/**
* Answers an array of data attributes for the field.
*
Expand Down Expand Up @@ -780,7 +801,11 @@ protected function getFieldConfig()
$data = [];

foreach ($values as $value) {
$data[] = $this->getResultData($this->getValueRecord($value), true);

if ($record = $this->getValueRecord($value)) {
$data[] = $this->getResultData($record, true);
}

}

$config['data'] = $data;
Expand Down
88 changes: 88 additions & 0 deletions src/Forms/Select2Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

use SilverStripe\Core\Convert;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FormField;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DataObjectInterface;
use SilverStripe\ORM\Relation;
Expand Down Expand Up @@ -91,6 +93,74 @@ public function Type()
return sprintf('select2field %s', parent::Type());
}

/**
* Renders the field for the template.
*
* @param array $properties
*
* @return DBHTMLText
*/
public function Field($properties = [])
{
// Merge Options:

$properties = array_merge($properties, [
'Options' => $this->getOptions()
]);

// Render Field:

return FormField::Field($properties);
}

/**
* Answers an array list containing the options for the field.
*
* @return ArrayList
*/
public function getOptions()
{
// Create Options List:

$options = ArrayList::create();

// Iterate Source Items:

foreach ($this->getSourceEmpty() as $value => $title) {
$options->push($this->getFieldOption($value, $title));
}

// Handle Tags:

if ($this->usesTags()) {

// Obtain Source Values:

$values = $this->getSourceValues();

// Iterate Value Array:

foreach ($this->getValueArray() as $value) {

// Handle Tag Values:

if (!in_array($value, $values)) {
$options->push($this->getFieldOption($value, $value));
}

}

}

// Apply Extensions:

$this->extend('updateOptions', $options);

// Answer Options List:

return $options;
}

/**
* Defines either the named config value, or the config array.
*
Expand Down Expand Up @@ -170,6 +240,18 @@ public function isMultiple()
return $this->getMultiple();
}

/**
* Answers true if the field is configured to use tags.
*
* @return boolean
*/
public function usesTags()
{
$config = $this->getFieldConfig();

return (isset($config['tags']) && $config['tags']);
}

/**
* Answers an array of HTML attributes for the field.
*
Expand Down Expand Up @@ -372,6 +454,12 @@ public function saveIntoRelation(Relation $relation)
*/
public function validate($validator)
{
// Baily Early (if tags are used):

if ($this->usesTags()) {
return true;
}

// Call Parent Method (if not multiple):

if (!$this->isMultiple()) {
Expand Down

0 comments on commit 3e5a33a

Please sign in to comment.