XF 2.3 Need help with some code

CipherX

New member
I am building an addon that uses widgets. I have my admin page pulling all the widgets. Now I want to just get the custom widgets to display. Where in my code did I got wrong?

Code:
public function actionIndex($positions)
    {
        $widgetRepo = $this->getWidgetRepo();

        $widgetsFinder = $widgetRepo->findWidgetsForList();
        $widgets = $widgetsFinder->fetch();

        foreach ($positions AS $position) {
            $positionId = $position->position_id;  // Example: Access the position ID
            $positionName = $position->custom_widget_sidebar;      // Example: Access the position name
            $isActive = $position->active;

        }

        $groupedWidgets = $widgetRepo->groupWidgetsByPositions($widgets, $totalWidgets);

        $positionsFinder = $widgetRepo->findWidgetPositionsForList();
        $positions = $positionsFinder->fetch();
        $positions[''] = null;

        /** @var TYPE_NAME $positionId */
        /** @var TYPE_NAME $positionName */
        /** @var TYPE_NAME $isActive */
        $viewParams = [
            'positionId' => $positionId,
            'positionName' => $positionName,
            'isActive' => $isActive,
            'groupedWidgets' => $groupedWidgets,
            'positions' => $positions,
            'totalWidgets' => $totalWidgets,
        ];
        return $this->view('XF:Widget\Listing', 'custom_widget_list', $viewParams);
    }

Maybe I am approaching it wrong. Any help would be much appreciated!
 
There are few things that do not seem to work in your code.
  1. The positions parameter to the index function, where does it come from ? Typically you should only be having one parameter that could come from an url and in that case it is typed ParameterBag.
  2. Your first foreach will just keep updating the 3 values so if you have multiple positions only one will be in those variables.
 
Back
Top Bottom