private function getRelationshipsTo(
        string $fieldHandle,
        array $fieldInfo,
        string $sectionHandle,
        int $id = null
    ): ?array {

        if (!empty($fieldInfo[$fieldHandle]['to'])) {
            try {
                $sexyFieldInstructions = $this->getSexyFieldRelationshipInstructions($fieldInfo, $fieldHandle);

                // You can have a different name for elements through config
                $nameExpression = [];
                if (!empty($sexyFieldInstructions) &&
                    !empty($sexyFieldInstructions['name-expression'])
                ) {
                    $nameExpression = explode('|', $sexyFieldInstructions['name-expression']);
                }

                // Maybe you want to add some more fields to the relationship content
                $addFields = [];
                if (!empty($sexyFieldInstructions) &&
                    !empty($sexyFieldInstructions['add-fields'])
                ) {
                    $addFields = $sexyFieldInstructions['add-fields'];
                }

                $to = $this->readSection->read(
                    $this->getRelationshipReadOptions($fieldHandle, $fieldInfo, $sexyFieldInstructions)
                );
                $fieldInfo[$fieldHandle][$fieldInfo[$fieldHandle]['to']] = [];

                /** @var CommonSectionInterface $entry */
                foreach ($to as $entry) {

                    // Map the name-expression to override naming
                    $name = $entry->getDefault();

                    if ($nameExpression) {
                        $find = $entry;
                        foreach ($nameExpression as $method) {
                            if ($find) {
                                $find = $find->$method();
                            }
                        }
                        if ($find) {
                            $name = $find;
                        }
                    }

                    // Default relationship data
                    $data = [
                        'id' => $entry->getId(),
                        'slug' => (string) $entry->getSlug(),
                        'name' => $name,
                        'created' => $entry->getCreated(),
                        'updated' => $entry->getUpdated(),
                        'selected' => false
                    ];

                    // Add data if configured
                    foreach ($addFields as $field) {
                        $method = 'get' . ucfirst($field);
                        $data[$field] = (string) $entry->{$method}();
                    }

                    $fieldInfo[$fieldHandle][$fieldInfo[$fieldHandle]['to']][] = $data;
                }
            } catch (EntryNotFoundException $exception) {
                $fieldInfo[$fieldHandle][$fieldInfo[$fieldHandle]['to']]['error'] = $exception->getMessage();
            }

            if (!empty($id)) {
                $fieldInfo = $this->setSelectedRelationshipsTo(
                    $sectionHandle,
                    $fieldHandle,
                    $fieldInfo,
                    $id
                );
            }
        }

        return $fieldInfo;
    }
private function getRelationshipsTo(
        string $fieldHandle,
        array $fieldInfo,
        string $sectionHandle,
        int $id = null
    ): ?array {

        if (!empty($fieldInfo[$fieldHandle]['to'])) {
            try {
                $sexyFieldInstructions = $this->getSexyFieldRelationshipInstructions($fieldInfo, $fieldHandle);

                // You can have a different name for elements through config
                $nameExpression = [];
                if (!empty($sexyFieldInstructions) &&
                    !empty($sexyFieldInstructions['name-expression'])
                ) {
                    $nameExpression = explode('|', $sexyFieldInstructions['name-expression']);
                }

                // Maybe you want to add some more fields to the relationship content
                $addFields = [];
                if (!empty($sexyFieldInstructions) &&
                    !empty($sexyFieldInstructions['add-fields'])
                ) {
                    $addFields = $sexyFieldInstructions['add-fields'];
                }

                $to = $this->readSection->read(
                    $this->getRelationshipReadOptions($fieldHandle, $fieldInfo, $sexyFieldInstructions)
                );
                $fieldInfo[$fieldHandle][$fieldInfo[$fieldHandle]['to']] = [];

                /** @var CommonSectionInterface $entry */
                foreach ($to as $entry) {

                    // Map the name-expression to override naming
                    $name = $entry->getDefault();

                    if ($nameExpression) {
                        $find = $entry;
                        foreach ($nameExpression as $method) {
                            if ($find) {
                                $find = $find->$method();
                            }
                        }
                        if ($find) {
                            $name = $find;
                        }
                    }

                    // Default relationship data
                    $data = [
                        'id' => $entry->getId(),
                        'slug' => (string) $entry->getSlug(),
                        'name' => $name,
                        'created' => $entry->getCreated(),
                        'updated' => $entry->getUpdated(),
                        'selected' => false
                    ];

                    // Add data if configured
                    foreach ($addFields as $field) {
                        $method = 'get' . ucfirst($field);
                        $data[$field] = (string) $entry->{$method}();
                    }

                    $fieldInfo[$fieldHandle][$fieldInfo[$fieldHandle]['to']][] = $data;
                }
            } catch (EntryNotFoundException $exception) {
                $fieldInfo[$fieldHandle][$fieldInfo[$fieldHandle]['to']]['error'] = $exception->getMessage();
            }

            if (!empty($id)) {
                $fieldInfo = $this->setSelectedRelationshipsTo(
                    $sectionHandle,
                    $fieldHandle,
                    $fieldInfo,
                    $id
                );
            }
        }

        return $fieldInfo;
    }
private function getRelationshipsTo ( string $ fieldHandle , array $ fieldInfo , string $ sectionHandle , int $ id = null ) : ? array { if ( ! empty ( $ fieldInfo [ $ fieldHandle ] [ 'to' ] ) ) { try { $ sexyFieldInstructions = $ this -> getSexyFieldRelationshipInstructions ( $ fieldInfo , $ fieldHandle ) ; // You can have a different name for elements through config $ nameExpression = [ ] ; if ( ! empty ( $ sexyFieldInstructions ) && ! empty ( $ sexyFieldInstructions [ 'name-expression' ] ) ) { $ nameExpression = explode ( '|' , $ sexyFieldInstructions [ 'name-expression' ] ) ; } // Maybe you want to add some more fields to the relationship content $ addFields = [ ] ; if ( ! empty ( $ sexyFieldInstructions ) && ! empty ( $ sexyFieldInstructions [ 'add-fields' ] ) ) { $ addFields = $ sexyFieldInstructions [ 'add-fields' ] ; } $ to = $ this -> readSection -> read ( $ this -> getRelationshipReadOptions ( $ fieldHandle , $ fieldInfo , $ sexyFieldInstructions ) ) ; $ fieldInfo [ $ fieldHandle ] [ $ fieldInfo [ $ fieldHandle ] [ 'to' ] ] = [ ] ; /** @var CommonSectionInterface $entry */ foreach ( $ to as $ entry ) { // Map the name-expression to override naming $ name = $ entry -> getDefault ( ) ; if ( $ nameExpression ) { $ find = $ entry ; foreach ( $ nameExpression as $ method ) { if ( $ find ) { $ find = $ find -> $ method ( ) ; } } if ( $ find ) { $ name = $ find ; } } // Default relationship data $ data = [ 'id' => $ entry -> getId ( ) , 'slug' => ( string ) $ entry -> getSlug ( ) , 'name' => $ name , 'created' => $ entry -> getCreated ( ) , 'updated' => $ entry -> getUpdated ( ) , 'selected' => false ] ; // Add data if configured foreach ( $ addFields as $ field ) { $ method = 'get' . ucfirst ( $ field ) ; $ data [ $ field ] = ( string ) $ entry -> { $ method } ( ) ; } $ fieldInfo [ $ fieldHandle ] [ $ fieldInfo [ $ fieldHandle ] [ 'to' ] ] [ ] = $ data ; } } catch ( EntryNotFoundException $ exception ) { $ fieldInfo [ $ fieldHandle ] [ $ fieldInfo [ $ fieldHandle ] [ 'to' ] ] [ 'error' ] = $ exception -> getMessage ( ) ; } if ( ! empty ( $ id ) ) { $ fieldInfo = $ this -> setSelectedRelationshipsTo ( $ sectionHandle , $ fieldHandle , $ fieldInfo , $ id ) ; } } return $ fieldInfo ; }
Get entries to populate a relationships field

@param string $fieldHandle
@param array $fieldInfo
@param string $sectionHandle
@param int|null $id
@return array|null
Get entries to populate a relationships field