Skip to content
Snippets Groups Projects

Remove route override and use controller inheritance for clearer code

Open Ludovic Favre requested to merge issue/lpb-3484245:3484245-refactoring into 1.0.x
Files
5
@@ -29,6 +29,20 @@ class LPBController extends ChooseComponentController implements ContainerInject
*/
protected $formBuilder;
/**
* The Layout Paragraphs Layout.
*
* @var \Drupal\layout_paragraphs\LayoutParagraphsLayout
*/
protected $layout_paragraph_layout;
/**
* The context for the component to be added.
*
* @var array
*/
protected $context;
/**
* LPBController constructor.
*
@@ -59,36 +73,34 @@ class LPBController extends ChooseComponentController implements ContainerInject
* Override the methods you need from ChooseComponentController.
*/
public function list(Request $request, LayoutParagraphsLayout $layout_paragraphs_layout): AjaxResponse|array {
$context = $this->getContextFromRequest($request);
// If inserting a new item adjacent to a sibling component, the region
// passed in the URL will be incorrect if the existing sibling component
// was dragged into another region. In that case, always use the existing
// sibling's region.
if ($context['sibling_uuid']) {
$sibling = $layout_paragraphs_layout->getComponentByUuid($context['sibling_uuid']);
$context['region'] = $sibling->getRegion();
}
$types = $this->getAllowedComponentTypes($layout_paragraphs_layout, $context);
// If there is only one type to render,
// return the component form instead of a list of links.
if (count($types) === 1) {
return $this->componentForm(key($types), $layout_paragraphs_layout, $context);
}
$this->layout_paragraph_layout = $layout_paragraphs_layout;
$this->context = $this->getContextFromRequest($request);
return parent::list($request, $layout_paragraphs_layout);
}
// If there is no browser selected, either it's not our widget or it's
// misconfigured. In both cases, fall back to the layout_paragraphs-widget.
$browser = $layout_paragraphs_layout->getSetting('paragraphs_browser', FALSE);
if ($browser === FALSE) {
return parent::list($request, $layout_paragraphs_layout);
}
/**
* Override parent componentMenu method.
*
* @param array $types
* @return array|AjaxResponse
*/
protected function componentMenu(array $types) {
// If we have no Browser, fall back to the default controller.
$paragraphs_browser_type = BrowserType::load($browser);
if ($paragraphs_browser_type === NULL) {
return parent::list($request, $layout_paragraphs_layout);
// getting the lpb setting.
$browser = $this->layout_paragraph_layout->getSetting('paragraphs_browser', FALSE);
// If there is a layout paragraphs browser widget set.
if($browser) {
$paragraphs_browser_type = BrowserType::load($browser);
// if the browser type is set.
if ($paragraphs_browser_type !== NULL) {
return $this->paragraphsBrowser($types, $paragraphs_browser_type, $this->layout_paragraph_layout, $this->context);
}
}
return $this->paragraphsBrowser($types, $paragraphs_browser_type, $layout_paragraphs_layout, $context);
// otherwise return default layout paragraphs widget.
return parent::componentMenu($types);
}
/**
Loading