How to get value from FlexForm to Controller
Asked Answered
G

3

8

I'm practicing on a very easy Extbase Extension and used a FlexForm to get three formula fields.
One of them is called "code" which should go to the EmbedderController.php and then to the viewer List.html.

I checked all tutorials I could find.

I don't understand how to get the FlexForm-value "code" into my Controller.
I get an empty page or don't get any value.

This is my FlexForm: Embedder.xml

<T3DataStructure>
        <meta type="array">
                <langChildren>0</langChildren>
                <langDisable>1</langDisable>
        </meta>
        <ROOT>
                <type>array</type>
                <el>
                        <settings.code>
                                <TCEforms>
                                        <label>Video Code</label>
                                        <config>
                                                <type>input</type>
                                                <size>20</size>
                                                <max>30</max>
                                                <eval>trim</eval>
                                        </config>
                                </TCEforms>
                        </settings.code>
                        <settings.width>
                                <TCEforms>
                                        <exclude>1</exclude>
                                        <label>Breite in Pixel</label>
                                        <config>
                                                <type>input</type>
                                                <size>10</size>
                                                <max>10</max>
                                                <eval>trim</eval>
                                        </config>
                                </TCEforms>
                        </settings.width>
                        <settings.height>
                                <TCEforms>
                                        <exclude>1</exclude>
                                        <label>Höhe in Pixel</label>
                                        <config>
                                                <type>input</type>
                                                <size>10</size>
                                                <max>10</max>
                                                <eval>trim</eval>
                                        </config>
                                </TCEforms>
                        </settings.height>
                </el>
        </ROOT>
</T3DataStructure>

And this is my EmbedderController.php

<?php
namespace HhuMediathek\Hhumediathek\Controller;
     
/**
 * EmbedderController
 */
class EmbedderController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
     
        /**
         * embedderRepository
         *
         * @var \HhuMediathek\Hhumediathek\Domain\Repository\EmbedderRepository
         * @inject
         */
        protected $embedderRepository = NULL;
     
        /**
         * action list
         *
         * @return void
         */
        public function listAction() {
                $this->settings['code'];
        }
}

And this is the viewer List.html

<f:layout name="Default" />
<f:section name="main">
<iframe width='570' height='321' style='width: 570px; height: 321px; border: 1px solid #ccc;' src='//xxx.de/embed/{code}' frameborder='0' allowfullscreen></iframe>
    </f:section>
Gulgee answered 12/7, 2015 at 16:27 Comment(0)
G
12

Okay I could figure it out myself. For people who struggle with the same problem as I did:

My mistake was, that I didn't need the line $this->settings['code']; in the Controller at all but write {settings.code} in the viewer List.html instead of just {code}. It's completly different than I read it in my book and some tutorials but this actually worked.

Gulgee answered 12/7, 2015 at 17:12 Comment(1)
could you please add the resolve flag, because other than know where to look exactlyGloucester
I
1

The assignment of the view parameter is missing. Therefore change

public function listAction() {
    $this->settings['code'];
}

to

public function listAction() {
    $this->view->assign('code', $this->settings['code']);
}

This way {code} should be available in the view.

Ictinus answered 3/7, 2017 at 16:5 Comment(0)
C
0

I want not to be restricted to settings.*. I use the following script in my controller.

        /** @var ContentObjectRenderer $content */
        $content = $this->configurationManager->getContentObject();
        $flexFormString = $content->data['pi_flexform'];
        $flexFormRaw = GeneralUtility::xml2array($flexFormString);
        if (is_callable([FlexformUtilities::class,'arrayFlatten'])) {
            $flexFormSimple = FlexformUtilities::arrayFlatten( $flexFormRaw); // second Param is ['data','sDEF','lDEF','vDEF']
            $referenceUidsList = $flexFormSimple['referenceList'];
        } else {
            $referenceUidsList = (int)$flexFormRaw['data']['sDEF']['lDEF']['referenceList']['vDEF'];
        }

The Utilities-Class contains the following flattenArray-method


    protected const DEFAULT_FLATTEN_KEYS = ['data','sDEF','lDEF','vDEF'];

 
   /**
     * https://mcmap.net/q/87098/-how-to-flatten-a-multidimensional-array
     *
     * @param array $listFlatKeys
     * @param $array
     * @return array
     */
    public static function arrayFlatten( $array, &$listFlatKeys = self::DEFAULT_FLATTEN_KEYS)
    {

        if (!is_array($array)) {
            return $array;
        }

        if (count($array) === 1) {
            $key = array_key_first($array);
            $value = self::arrayFlatten( $array[$key],$listFlatKeys);
            if (in_array($key, $listFlatKeys)) {
                return $value;
            } else {
                return [$key => $value];
            }
        }

        $return = [];
        foreach ($array as $key => $value) {
            if (in_array($key, $listFlatKeys)) {
                $return[] = self::arrayFlatten( $value, $listFlatKeys);
            } else {
                $return[$key] = self::arrayFlatten( $value, $listFlatKeys);
            }
        }
        return $return;
    }

I use it although in a Alias-similiar viewhelper, to get information of a flex-field in the frontend.

Clientage answered 14/1, 2021 at 14:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.