Craft\NeoFieldType::prepValueFromPost PHP Method

prepValueFromPost() public method

Converts the field's input value from post data to what it should be stored as in the database.
public prepValueFromPost ( array $data ) : array
$data array
return array
    public function prepValueFromPost($data)
    {
        $blockTypes = craft()->neo->getBlockTypesByFieldId($this->model->id, 'handle');
        if (!is_array($data)) {
            return [];
        }
        $oldBlocksById = [];
        if (!empty($this->element->id)) {
            $ownerId = $this->element->id;
            $ids = [];
            foreach (array_keys($data) as $blockId) {
                if (is_numeric($blockId) && $blockId != 0) {
                    $ids[] = $blockId;
                }
            }
            if ($ids) {
                $criteria = craft()->elements->getCriteria(Neo_ElementType::NeoBlock);
                $criteria->fieldId = $this->model->id;
                $criteria->ownerId = $ownerId;
                $criteria->id = $ids;
                $criteria->limit = null;
                $criteria->status = null;
                $criteria->localeEnabled = null;
                $criteria->locale = $this->element->locale;
                $oldBlocks = $criteria->find();
                foreach ($oldBlocks as $oldBlock) {
                    $oldBlocksById[$oldBlock->id] = $oldBlock;
                }
            }
        } else {
            $ownerId = null;
        }
        $blocks = [];
        foreach ($data as $blockId => $blockData) {
            if (!isset($blockData['type']) || !isset($blockTypes[$blockData['type']])) {
                continue;
            }
            $blockType = $blockTypes[$blockData['type']];
            if (strncmp($blockId, 'new', 3) === 0 || !isset($oldBlocksById[$blockId])) {
                $block = new Neo_BlockModel();
                $block->modified = true;
                $block->fieldId = $this->model->id;
                $block->typeId = $blockType->id;
                $block->ownerId = $ownerId;
                $block->ownerLocale = $this->element->locale;
            } else {
                $block = $oldBlocksById[$blockId];
                $block->modified = isset($blockData['modified']) ? (bool) $blockData['modified'] : true;
            }
            $block->setOwner($this->element);
            $block->enabled = isset($blockData['enabled']) ? (bool) $blockData['enabled'] : true;
            $block->collapsed = isset($blockData['collapsed']) ? (bool) $blockData['collapsed'] : false;
            $block->level = (isset($blockData['level']) ? intval($blockData['level']) : 0) + 1;
            $ownerContentPostLocation = $this->element->getContentPostLocation();
            if ($ownerContentPostLocation) {
                $block->setContentPostLocation("{$ownerContentPostLocation}.{$this->model->handle}.{$blockId}.fields");
            }
            if (isset($blockData['fields'])) {
                $block->setContentFromPost($blockData['fields']);
            }
            $blocks[] = $block;
        }
        return $blocks;
    }