Bolt\Storage\Field\Collection\FieldCollection::doInitialize PHP Method

doInitialize() protected method

Handles the conversion of references to entities.
protected doInitialize ( )
    protected function doInitialize()
    {
        $objects = [];
        if ($this->references) {
            $repo = $this->em->getRepository('Bolt\\Storage\\Entity\\FieldValue');
            $instances = $repo->findBy(['id' => $this->references]);
            foreach ((array) $instances as $val) {
                $fieldtype = $val->getFieldtype();
                $field = $this->em->getFieldManager()->getFieldFor($fieldtype);
                $type = $field->getStorageType();
                $typeCol = 'value_' . $type->getName();
                // Because there's a potential for custom fields that use json storage to 'double hydrate' this causes
                // json_decode to throw a warning. Here we prevent that by replacing the error handler.
                set_error_handler(function ($errNo, $errStr, $errFile) {
                }, E_WARNING);
                $hydratedVal = $this->em->getEntityBuilder($val->getContenttype())->getHydratedValue($val->{$typeCol}, $val->getName(), $val->getFieldname());
                restore_error_handler();
                // If we do not have a hydrated value returned then we fall back to the one passed in
                if ($hydratedVal) {
                    $val->setValue($hydratedVal);
                } else {
                    $val->setValue($val->{$typeCol});
                }
                $objects[$val->getFieldname()] = $val;
            }
        }
        $this->collection = new ArrayCollection($objects);
        $this->em = null;
    }