/** * Usage [e.g. in getCMSFields] * $field = new PickerField('Authors', 'Selected Authors', $this->Authors(), 'Select Author(s)'); * * @param string $name - Name of field (typically the relationship method) * @param string $title - GridField Title * @param SS_List $dataList - Result of the relationship component method (E.g. $this->Authors()) * @param string $linkExistingTitle - AddExisting Button Title * @param string $sortField - Field to sort on. Be sure it exists in the $many_many_extraFields static */ public function __construct($name, $title = null, SS_List $dataList = null, $linkExistingTitle = null, $sortField = null) { $config = GridfieldConfig::create()->addComponents(new GridFieldButtonRow('before'), new GridFieldToolbarHeader(), new GridFieldDataColumns(), new GridFieldTitleHeader(), new GridFieldPaginator(), new PickerFieldAddExistingSearchButton(), new PickerFieldDeleteAction()); if ($sortField) { $config->addComponent(new GridFieldOrderableRows($sortField)); } if (!$linkExistingTitle) { $linkExistingTitle = $this->isHaveOne() ? 'Select a ' . $dataList->dataClass() : 'Select ' . $dataList->dataClass() . '(s)'; // plural [has_many, many_many] } $config->getComponentByType('PickerFieldAddExistingSearchButton')->setTitle($linkExistingTitle); return parent::__construct($name, $title, $dataList, $config); }
/** * Returns a data class that is a DataObject type that this GridField should look like. * * @return string * * @throws LogicException */ public function getModelClass() { if ($this->modelClassName) { return $this->modelClassName; } if ($this->list && method_exists($this->list, 'dataClass')) { $class = $this->list->dataClass(); if ($class) { return $class; } } throw new LogicException('GridField doesn\'t have a modelClassName, so it doesn\'t know the columns of this grid.'); }
/** * @return array */ public function getModelClasses() { if ($this->modelClassNames) { return $this->modelClassNames; } if ($this->list && method_exists($this->list, 'dataClass')) { $class = $this->list->dataClass(); if ($class) { if (!is_array($class)) { $class = array($class); } return static::convert_to_associative($class); } } return array(); }
/** * Gets the table which contains the sort field. * * @param DataList $list * @return string */ public function getSortTable(SS_List $list) { $field = $this->getSortField(); if ($list instanceof ManyManyList) { $extra = $list->getExtraFields(); $table = $list->getJoinTable(); if ($extra && array_key_exists($field, $extra)) { return $table; } } $classes = ClassInfo::dataClassesFor($list->dataClass()); foreach ($classes as $class) { if (singleton($class)->hasOwnTableDatabaseField($field)) { return $class; } } throw new Exception("Couldn't find the sort field '{$field}'"); }