/** * Prep value for CP. * * @param mixed $value * * @return mixed */ private function prepValueForCp($value) { // Overwrite value, if any if ($value) { // Unset value $value = null; // Fetch target id(s) $results = craft()->db->createCommand()->select('targetId')->from('relations')->where(array('fieldId' => $this->model->id, 'sourceId' => $this->element->id))->queryAll(); // If db result is valid if ($results && is_array($results)) { // Gather value $value = array(); // Loop through target ids foreach ($results as $result) { $value[] = $result['targetId']; } } } // Return with new values return parent::prepValue($value); }
/** * Return criteria in back-end and YT ID(s) in front-end. * * @param mixed $value * * @return ElementCriteriaModel|array */ public function prepValue($value) { // Behave as normal asset in back-end if (craft()->request->isCpRequest()) { // Overwrite value, if any if ($value) { // Fetch target id(s) $results = craft()->db->createCommand()->select('targetId')->from('relations')->where(array('fieldId' => $this->model->id, 'sourceId' => $this->element->id))->queryAll(); // If db result is valid if ($results && is_array($results)) { // Gather value $value = array(); // Loop through target ids foreach ($results as $result) { $value[] = $result['targetId']; } } else { // Else return nothing $value = null; } } // Return with new values return parent::prepValue($value); } // Value is always an array if (!is_array($value)) { $value = array(); } // Prepare for models $videos = array(); foreach ($value as $id) { $videos[] = array('id' => $id); } // Return video models return YouTube_VideoModel::populateModels($videos); }