public function Field($properties = array()) { Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js'); Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js'); Requirements::javascript(DYNAMICLIST_MODULE . '/javascript/DependentDynamicListDropdownField.js'); $listItems = array(); if (is_string($this->dependentLists)) { $list = DynamicList::get_dynamic_list($this->dependentLists); if ($list) { $this->dependentLists = $list->Items()->map('Title', 'Title')->toArray(); } } if (!is_array($this->dependentLists)) { $this->dependentLists = array(); } foreach ($this->dependentLists as $k => $v) { $list = DynamicList::get_dynamic_list($k); if ($list) { $listItems[$k] = $list->Items()->map('Title', 'Title')->toArray(); } } $this->setAttribute('data-listoptions', Convert::raw2json($listItems)); $this->setAttribute('data-dependentOn', $this->dependentOn); if ($this->value) { $this->setAttribute('data-initialvalue', $this->value); } return parent::Field(); }
function __construct($name, $title = null, $source = null, $value = "", $form = null, $emptyString = null) { if (!$source) { $source = array(); } if (is_string($source)) { // it should be the name of a list, lets get all its contents $dynamicList = DynamicList::get_dynamic_list($source); $source = array(); if ($dynamicList) { $items = $dynamicList->Items(); foreach ($items as $item) { $source[$item->Title] = $item->Title; } } } $this->addExtraClass('dropdown'); parent::__construct($name, $title, $source, $value, $form, $emptyString); }
protected function processRecord($record, $columnMap, &$results, $preview = false) { $class = $this->objectClass; $title = trim($record['Title']); $item = trim($record['ListItem']); $existingList = DynamicList::get_dynamic_list($title); if (!$existingList) { $existingList = new DynamicList(); $existingList->Title = $title; $existingList->write(); } // now add the item to that list $existingItem = DataObject::get_one('DynamicListItem', '"Title"=\'' . Convert::raw2sql($item) . '\' AND "ListID" = ' . (int) $existingList->ID); if (!$existingItem) { $existingItem = new DynamicListItem(); $existingItem->Title = $item; $existingItem->ListID = $existingList->ID; $existingItem->write(); } }
function getFormField() { $sourceList = $this->getSetting('SourceList') ? $this->getSetting('SourceList') : null; // first off lets go and output all the options we need $fields = $this->Parent()->Fields(); $source = null; foreach ($fields as $field) { if ($field->Name == $sourceList) { $source = $field; break; } } $optionLists = array(); if ($source) { // all our potential lists come from the source list's dynamic list source, so we need to go load that // first, then iterate it and build all the additional required lists $sourceList = DynamicList::get_dynamic_list($source->getSetting('ListTitle')); if ($sourceList) { $items = $sourceList->Items(); // now lets create a bunch of option fields foreach ($items as $sourceItem) { // now get the dynamic list that is represented by this one $list = DynamicList::get_dynamic_list($sourceItem->Title); if ($list) { $optionLists[$sourceItem->Title] = $sourceItem->Title; } } } if (count($optionLists)) { return new DependentDynamicListDropdownField($this->Name, $this->Title, $optionLists, $source->Name); } else { return new DropdownField($this->Name, $this->Title, array()); } } // return a new list return new LiteralField($this->Name); }
/** * Override method for validation to use dynamic list based off the * parent's value. Overridden due to null source. * @param type $validator * @return bool */ public function validate($validator) { // Source isn't pulled in correctly and we're going to rectify this // later on, so this can be an empty array for now. $source = array(); $disabled = $this->getDisabledItems(); // Grab the parent list we're trying to validate against first so we can refer to it. $parentListName = $this->getForm()->Fields()->fieldByName($this->dependentOn)->value; // Use the items from the Dynamic list as the "source" for validation purposes $parentList = DynamicList::get_dynamic_list($parentListName); if ($parentList) { $source = $parentList->Items()->map('Title', 'Title')->toArray(); } // Carry on as normal validating against our new source! // Since there's no data if the list doesn't exist, then of course it will fail if (!array_key_exists($this->value, $source) || in_array($this->value, $disabled)) { if ($this->getHasEmptyDefault() && !$this->value) { return true; } $validator->validationError($this->name, _t('DropdownField.SOURCE_VALIDATION', "Please select a value within the list provided. {value} is not a valid option", array('value' => $this->value)), "validation"); return false; } return true; }