Instantiates a field object.

@param array $options
@param bool  $loadRelationships

@return Frozennode\Administrator\Fields\Field
553818
public function getFieldObject($options)
    {
        $class = $this->getFieldTypeClass($options['type']);

        return new $class($this->validator, $this->config, $this->db, $options);
    }
481655
public function loadRelationshipOptions(&$options)
    {
        //if we want all of the possible items on the other model, load them up, otherwise leave the options empty
        $items        = array();
        $model        = $this->config->getDataModel();
        $relationship = $model->{$options['field_name']}();
        $relatedModel = $relationship->getRelated();

        if ($this->validator->arrayGet($options, 'load_relationships')) {
            //if a sort field was supplied, order the results by it
            if ($optionsSortField = $this->validator->arrayGet($options, 'options_sort_field')) {
                $optionsSortDirection = $this->validator->arrayGet($options, 'options_sort_direction', $this->defaults['options_sort_direction']);

                $query = $relatedModel->orderBy($this->db->raw($optionsSortField), $optionsSortDirection);
            }
            //otherwise just pull back an unsorted list
            else {
                $query = $relatedModel->newQuery();
            }

            //run the options filter
            $options['options_filter']($query);

            //get the items
            $items = $query->get();
        }
        //otherwise if there are relationship items, we need them in the initial options list
        elseif ($relationshipItems = $relationship->get()) {
            $items = $relationshipItems;

            // if no related items exist, add default item, if set in options
            if (count($items) == 0 && array_key_exists('value', $options)) {
                $items = $relatedModel->where($relatedModel->getKeyName(), '=', $options['value'])->get();
            }
        }

        //map the options to the options property where array('id': [key], 'text': [nameField])
        $nameField          = $this->validator->arrayGet($options, 'name_field', $this->defaults['name_field']);
        $keyField           = $relatedModel->getKeyName();
        $options['options'] = $this->mapRelationshipOptions($items, $nameField, $keyField);
    }
575916
public function with($relationships)
    {
        if (! is_array($relationships)) {
            $relationships = explode(', ', $relationships);
        }

        if (! in_array($relationships, $this->withRelationships)) {
            foreach ($relationships as $with) {
                $this->withRelationships[] = $with;
            }
        }

        return $this;
    }
228117
protected function getRelationalField($relationalArray)
    {
        list($relation, $field) = $relationalArray;
        if (! method_exists($this, $relation)) {
            throw new RelationshipNotDefinedException();
        }

        return $this[$relation][$field];
    }
1297995
public function mapRelationshipOptions($items, $nameField, $keyField)
    {
        $result = array();

        foreach ($items as $option) {
            $result[] = array(
                'id'   => $option->{$keyField},
                'text' => strval($option->{$nameField}),
            );
        }

        return $result;
    }
256329
protected static function _relationsToLoad() {
		try {
			if (!$connection = static::connection()) {
				return;
			}
		} catch (ConfigException $e) {
			return;
		}

		if (!$connection::enabled('relationships')) {
			return;
		}

		$self = static::_object();

		foreach ($self->_relationTypes as $type) {
			$self->$type = Set::normalize($self->$type);
			foreach ($self->$type as $name => $config) {
				$self->_relationsToLoad[$name] = $type;
				$fieldName = $self->_relationFieldName($type, $name);
				$self->_relationFieldNames[$fieldName] = $name;
			}
		}
	}
586032
protected function loadRelation(array $models, $name, Closure $constraints)
    {
        $relation = $this->getRelation($name);

        $relation->addEagerConstraints($models);

        call_user_func($constraints, $relation);

        $models = $relation->initRelation($models, $name);

        $results = $relation->getEager();

        return $relation->match($models, $results, $name);
    }
691130
public function load($relations)
    {
        if ($this->isNotEmpty()) {
            if (is_string($relations)) {
                $relations = func_get_args();
            }

            $query = $this->first()->newQueryWithoutRelationships()->with($relations);

            $this->items = $query->eagerLoadRelations($this->items);
        }

        return $this;
    }
520382
protected function loadRelation(array $results, string $name, Closure $constraints) : array
    {
        // First we will "back up" the existing where conditions on the query so we can
        // add our eager constraints. Then we will merge the wheres that were on the
        // query back to it in order that any where conditions might be specified.
        $relation = $this->getRelation($name);

        $relation->addEagerConstraints($results);

        call_user_func($constraints, $relation);

        // Once we have the results, we just match those back up to their parent models
        // using the relationship instance. Then we just return the finished arrays
        // of models which have been eagerly hydrated and are readied for return.
        return $relation->match($results, $name);
    }
1368640
public function relationRender($field, $options = [])
    {
        /*
         * Session key
         */
        if (is_string($options)) {
            $options = ['sessionKey' => $options];
        }

        if (isset($options['sessionKey'])) {
            $this->sessionKey = $options['sessionKey'];
        }

        /*
         * Apply options and extra config
         */
        $allowConfig = ['readOnly', 'recordUrl', 'recordOnClick'];
        $extraConfig = array_only($options, $allowConfig);
        $this->extraConfig = $extraConfig;
        $this->applyExtraConfig($extraConfig, $field);

        /*
         * Initialize
         */
        $this->validateField($field);
        $this->prepareVars();

        /*
         * Determine the partial to use based on the supplied section option
         */
        $section = $options['section'] ?? null;
        switch (strtolower($section)) {
            case 'toolbar':
                return $this->toolbarWidget ? $this->toolbarWidget->render() : null;

            case 'view':
                return $this->relationMakePartial('view');

            default:
                return $this->relationMakePartial('container');
        }
    }
1285173
protected function eagerLoadRelation(array $models, $name, Closure $constraints)
    {
        // First we will "back up" the existing where conditions on the query so we can
        // add our eager constraints. Then we will merge the wheres that were on the
        // query back to it in order that any where conditions might be specified.
        $relation = $this->getRelation($name);

        $relation->addEagerConstraints($models);

        $constraints($relation);

        // Once we have the results, we just match those back up to their parent models
        // using the relationship instance. Then we just return the finished arrays
        // of models which have been eagerly hydrated and are readied for return.
        return $relation->match(
            $relation->initRelation($models, $name),
            $relation->getEager(), $name
        );
    }
495550
public function serialize(Field $field, array $options = [])
    {
        $serialized = [
            'id' => $field->getUuid(),
            'autoId' => $field->getId(),
            'name' => $field->getName(),
            'label' => $field->getName(),
            'type' => $field->getFieldType(),
            'required' => $field->isRequired(),
            'help' => $field->getHelp(),
            'restrictions' => [
                'isMetadata' => $field->getIsMetadata(),
                'locked' => $field->isLocked(),
                'lockedEditionOnly' => $field->getLockedEditionOnly(),
                'hidden' => $field->isHidden(),
                'order' => $field->getOrder(),
            ],
        ];

        if (count($field->getDetails()) > 0) {
            $serialized['options'] = $field->getDetails();
        }
        if (in_array($field->getType(), [FieldFacet::CHOICE_TYPE, FieldFacet::CASCADE_TYPE])) {
            $serialized['options']['choices'] = array_map(function (FieldFacetChoice $choice) {
                return $this->fieldFacetChoiceSerializer->serialize($choice);
            }, $field->getFieldFacet()->getRootFieldFacetChoices());
        }
        if (!in_array(Options::SERIALIZE_MINIMAL, $options)) {
            $serialized = array_merge($serialized, [
                'fieldFacet' => $this->fieldFacetSerializer->serialize($field->getFieldFacet()),
            ]);
        } else {
            $serialized = array_merge($serialized, [
                'fieldFacet' => [
                    'id' => $field->getUuid(),
                ],
            ]);
        }

        return $serialized;
    }
755993
protected function _linkField($options)
    {
        $links = [];
        $name = $this->alias;

        if ($options['foreignKey'] === false && $this->associationType === Association::ONE_TO_MANY) {
            $msg = 'Cannot have foreignKey = false for hasMany associations. ' .
                   'You must provide a foreignKey column.';
            throw new RuntimeException($msg);
        }

        $keys = in_array($this->associationType, [Association::ONE_TO_ONE, Association::ONE_TO_MANY]) ?
            $this->foreignKey :
            $this->bindingKey;

        foreach ((array)$keys as $key) {
            $links[] = sprintf('%s.%s', $name, $key);
        }

        if (count($links) === 1) {
            return $links[0];
        }

        return $links;
    }
1187734
protected static function _initRelationships()
    {
        $className = get_called_class();
        if (!empty(static::$_classRelationships[$className])) {
            $relationships = [];
            foreach (static::$_classRelationships[$className] as $relationship => $options) {
                if (is_array($options)) {
                    $relationships[$relationship] = static::_initRelationship($relationship, $options);
                }
            }
            static::$_classRelationships[$className] = $relationships;
        }
    }
678474
public function fromFullyQualifiedRelationships(array $relationshipNames)
    {
        foreach ($relationshipNames as $relationshipName) {
            $relationship = $this->getRelationshipResourceForReference($relationshipName);

            if ($relationship !== null) {
                return $relationship;
            }
        }

        return null;
    }
1133208
public function provide($data = null, array $options = [])
    {
        $result = null;

        if (empty($data)) {
            return $result;
        }

        $relatedProperties[] = $this->_getRelatedProperties($options['fieldDefinitions']->getLimit(), $data);

        if (!empty($relatedProperties[0]['config']['parent']['module'])) {
            array_unshift(
                $relatedProperties,
                $this->_getRelatedParentProperties($relatedProperties[0])
            );
        }

        $elementName = 'CsvMigrations.FieldHandlers/RelatedFieldHandler/view';
        $params = [
            'relatedProperties' => $relatedProperties,
            'options' => $options,
        ];

        return $this->renderElement($elementName, $params);
    }
145019
public function fieldLabels($includeRelations = true)
    {
        $labels = parent::fieldLabels($includeRelations);

        $labels['Title'] = _t(__CLASS__ . '.PageTitleLabel', 'Post Title');

        return $labels;
    }