Register a document.

@param mixed $document
@param NodeInterface $node
@param NodeInterface $node
@param string $locale

@throws DocumentManagerException
776543
public function registerDocument($document, NodeInterface $node, $locale)
    {
        $oid = $this->getObjectIdentifier($document);
        $uuid = $node->getIdentifier();

        // do not allow nodes without UUIDs or reregistration of documents
        $this->validateDocumentRegistration($document, $locale, $node, $oid, $uuid);

        $this->documentMap[$oid] = $document;
        $this->documentNodeMap[$oid] = $uuid;
        $this->nodeMap[$node->getIdentifier()] = $node;
        $this->nodeDocumentMap[$this->getNodeLocaleKey($node->getIdentifier(), $locale)] = $document;
        $this->documentLocaleMap[$oid] = $locale;
    }
876634
private function mapContentToNode($document, NodeInterface $node, $locale, $ignoreRequired)
    {
        $structure = $document->getStructure();
        $webspaceName = $this->inspector->getWebspace($document);
        $metadata = $this->inspector->getStructureMetadata($document);

        if (!$metadata) {
            throw new \RuntimeException(
                sprintf(
                    'Metadata for Structure Type "%s" was not found, does the file "%s.xml" exists?',
                    $document->getStructureType(),
                    $document->getStructureType()
                )
            );
        }

        foreach ($metadata->getProperties() as $propertyName => $structureProperty) {
            if (TitleSubscriber::PROPERTY_NAME === $propertyName) {
                continue;
            }

            $realProperty = $structure->getProperty($propertyName);
            $value = $realProperty->getValue();

            if (false === $ignoreRequired && $structureProperty->isRequired() && null === $value) {
                throw new MandatoryPropertyException(
                    sprintf(
                        'Property "%s" in structure "%s" is required but no value was given. Loaded from "%s"',
                        $propertyName,
                        $metadata->getName(),
                        $metadata->getResource()
                    )
                );
            }

            $contentTypeName = $structureProperty->getType();
            $contentType = $this->contentTypeManager->get($contentTypeName);

            // TODO: Only write if the property has been modified.

            $legacyProperty = $this->legacyPropertyFactory->createTranslatedProperty($structureProperty, $locale);
            $legacyProperty->setValue($value);

            $contentType->write(
                new SuluNode($node),
                $legacyProperty,
                null,
                $webspaceName,
                $locale,
                null
            );
        }
    }
1355181
public function registerManaged($document, $identifier, array $data)
    {
        $oid                              = spl_object_hash($document);
        $this->documentIdentifiers[$oid]  = $identifier !== null ? $identifier : $oid;
        $this->documentStates[$oid]       = self::STATE_MANAGED;
        $this->originalDocumentData[$oid] = $data;
        $this->addToIdentityMap($document);
    }
1085892
private function persistGeneric(
        NodeInterface $node,
        DocumentAccessor $accessor,
        $fieldName,
        $locale,
        array $fieldMapping
    ) {
        try {
            $phpcrName = $this->encoder->encode($fieldMapping['encoding'], $fieldMapping['property'], $locale);
            $value = $accessor->get($fieldName);
            $this->validateFieldValue($value, $fieldName, $fieldMapping);
            $node->setProperty($phpcrName, $value);
        } catch (InvalidLocaleException $ex) {
            // arguments invalid, no valid propertyname could be generated (e.g. no locale given for localized encoding)
            return;
        }
    }
1158187
private function persistReference(
        NodeInterface $node,
        DocumentAccessor $accessor,
        $fieldName,
        $locale,
        $fieldMapping
    ) {
        $referenceDocument = $accessor->get($fieldName);

        if (!$referenceDocument) {
            return;
        }

        if ($fieldMapping['multiple']) {
            throw new \InvalidArgumentException(
                sprintf(
                    'Mapping references as multiple not currently supported (when mapping "%s")',
                    $fieldName
                )
            );
        }

        try {
            $referenceNode = $this->documentRegistry->getNodeForDocument($referenceDocument);
            $phpcrName = $this->encoder->encode($fieldMapping['encoding'], $fieldMapping['property'], $locale);
            $node->setProperty($phpcrName, $referenceNode);
        } catch (InvalidLocaleException $ex) {
            // arguments invalid, no valid propertyname could be generated (e.g. no locale given for localized encoding)
            return;
        }
    }
246550
private function upgradeNode(NodeInterface $node, $locale, array $properties, $up)
    {
        /** @var BasePageDocument $document */
        $document = $this->documentManager->find($node->getIdentifier(), $locale);
        $documentLocales = $this->documentInspector->getLocales($document);

        if (!in_array($locale, $documentLocales)) {
            return;
        }

        foreach ($properties as $property) {
            if ($property['property'] instanceof BlockMetadata) {
                $this->upgradeBlockProperty($property['property'], $property['components'], $node, $locale, $up);
            } else {
                $this->upgradeProperty($property['property'], $node, $locale, $up);
            }
        }

        $this->documentManager->persist($document, $locale, ['auto_name' => false]);
    }
1150458
public function register(Node\NodeInterface $node)
	{
		if ( ! in_array($node, $this->array, true)) {
			$this->array[] = $node;
		}
	}
695164
public function getPageWithNodeInterface(Node $node, $locale)
    {
        $nodeTranslation = $node->getNodeTranslation($locale, true);
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();

        return $nodeVersion->getRef($this->em);
    }
877044
private function setMixins(Mapping\ClassMetadata $metadata, NodeInterface $node, $document)
    {
        $repository = $this->session->getRepository();
        if ($metadata->versionable === 'full') {
            if ($repository->getDescriptor(RepositoryInterface::OPTION_VERSIONING_SUPPORTED)) {
                $node->addMixin('mix:versionable');
            } elseif ($repository->getDescriptor(RepositoryInterface::OPTION_SIMPLE_VERSIONING_SUPPORTED)) {
                $node->addMixin('mix:simpleVersionable');
            }
        } elseif ($metadata->versionable === 'simple'
            && $repository->getDescriptor(RepositoryInterface::OPTION_SIMPLE_VERSIONING_SUPPORTED)
        ) {
            $node->addMixin('mix:simpleVersionable');
        }

        if (!$node->isNodeType('mix:referenceable') && $metadata->referenceable) {
            $node->addMixin('mix:referenceable');
        }

        // manually set the uuid if it is not present yet, so we can assign it to documents
        if ($node->isNodeType('mix:referenceable') && !$node->hasProperty('jcr:uuid')) {
            $uuid = false;

            $uuidFieldName = $metadata->getUuidFieldName();
            if ($uuidFieldName) {
                $uuid = $metadata->getFieldValue($document, $uuidFieldName);
            }

            if (!$uuid) {
                $uuid = $this->generateUuid();
            }

            $node->setProperty('jcr:uuid', $uuid);

            if ($uuidFieldName && !$metadata->getFieldValue($document, $uuidFieldName)) {
                $metadata->setFieldValue($document, $uuidFieldName, $uuid);
            }
        }
    }
759210
public function indexNode(Node $node, $lang)
    {
        $this->createNodeDocuments($node, $lang);

        if (!empty($this->documents)) {
            $this->searchProvider->addDocuments($this->documents);
            $this->documents = [];
        }
    }
583424
public function deregisterDocument($document)
    {
        $oid = $this->getObjectIdentifier($document);
        $this->assertDocumentExists($document);
        $nodeIdentifier = $this->documentNodeMap[$oid];
        $locale = $this->documentLocaleMap[$oid];

        unset($this->nodeMap[$nodeIdentifier]);
        unset($this->nodeDocumentMap[$this->getNodeLocaleKey($nodeIdentifier, $locale)]);
        unset($this->documentMap[$oid]);
        unset($this->documentNodeMap[$oid]);
        unset($this->documentLocaleMap[$oid]);
        unset($this->hydrationState[$oid]);
    }
447678
public function onPostSerialize(ObjectEvent $event)
    {
        $document = $event->getObject();

        if (!$document instanceof LocaleBehavior || !$this->documentRegistry->hasDocument($document)) {
            return;
        }

        $visitor = $event->getVisitor();

        $visitor->addData('availableLocales', $this->documentInspector->getLocales($document));
        $visitor->addData('contentLocales', $this->documentInspector->getConcreteLocales($document));

        $localizationState = $this->documentInspector->getLocalizationState($document);

        if (LocalizationState::GHOST === $localizationState) {
            $visitor->addData('type', ['name' => 'ghost', 'value' => $document->getLocale()]);
        }

        if (LocalizationState::SHADOW === $localizationState) {
            $visitor->addData('type', ['name' => 'shadow', 'value' => $document->getLocale()]);
        }
    }
1115002
public function registerNodeChange(NodeInterface $node)
    {
        if (in_array($node->getIdentifier(), $this->tagsToFlush)) {
            return;
        }
        if (!$node->getNodeType()->isOfType('Neos.Neos:Document')) {
            return;
        }
        $this->tagsToFlush[] = $node->getIdentifier();
    }
558552
public static void setDocumentElement(final Document document, final Element element) {
        Element documentElement = document.getDocumentElement();
        if (documentElement != null) {
            document.removeChild(documentElement);
        }
        if (element != null) {
            if (element.getOwnerDocument().equals(document)) {
                document.appendChild(element);
                return;
            }
            Node node = document.adoptNode(element);
            document.appendChild(node);
        }
    }
1037068
public function removeTranslation($document, $locale)
    {
        $class = $this->dm->getClassMetadata(get_class($document));
        if ($invoke = $this->eventListenersInvoker->getSubscribedSystems($class, Event::preRemoveTranslation)) {
            $this->eventListenersInvoker->invoke(
                $class,
                Event::preRemoveTranslation,
                $document,
                new LifecycleEventArgs($document, $this->dm),
                $invoke
            );
        }

        $metadata = $this->dm->getClassMetadata(get_class($document));
        if (!$this->isDocumentTranslatable($metadata)) {
            return;
        }

        if (1 === count($this->getLocalesFor($document))) {
            throw new PHPCRException('The last translation of a translatable document may not be removed');
        }

        if ($document instanceof Proxy) {
            $document->__load();
        }

        $oid = spl_object_hash($document);
        $this->documentTranslations[$oid][$locale] = null;
        $this->setLocale($document, $metadata, null);
    }
1226217
private function rehydrateDocument($document, NodeInterface $node, $locale)
    {
        $hydrateEvent = new HydrateEvent($node, $locale, ['rehydrate' => true]);
        $hydrateEvent->setDocument($document);
        $this->eventDispatcher->dispatch(Events::HYDRATE, $hydrateEvent);
    }
137338
private function persistDocument($data, $document, $locale)
    {
        $formType = $this->getMetadataFactory()->getMetadataForAlias('article')->getFormType();
        $form = $this->createForm(
            $formType,
            $document,
            [
                // disable csrf protection, since we can't produce a token, because the form is cached on the client
                'csrf_protection' => false,
            ]
        );
        $form->submit($data, false);

        if (!$form->isValid()) {
            throw new InvalidFormException($form);
        }

        if (array_key_exists('author', $data) && null === $data['author']) {
            $document->setAuthor(null);
        }

        if (array_key_exists('additionalWebspaces', $data) && null === $data['additionalWebspaces']) {
            $document->setAdditionalWebspaces(null);
        }

        $this->getDocumentManager()->persist(
            $document,
            $locale,
            [
                'user' => $this->getUser()->getId(),
                'clear_missing_content' => false,
            ]
        );
    }