How to show entries of custom entity as options for a selector in frontend in Shopware 5?

416 views Asked by At

I have created a custom entity Model with it's model and ext.js app as well. It lets me to create new entities in shopware backend as it is described here:

https://developers.shopware.com/developers-guide/backend-components/listing/

I then linked the entry to the s_user with s_user_attributes table as it is shown here:

https://developers.shopware.com/developers-guide/attribute-system/

$crud->update('s_user_attributes', 'recommendedStream', 'single_selection', [
'displayInBackend' => true,
'label' => 'Recommended stream',
'entity' => 'Shopware\Models\ProductStream\ProductStream',

This let's me to select the entity and see the list of all those entities in a selector in backend settings of a customer.

I want to know what is the easies and correct way to have a selector for user attributes with options as all the entries which can be created in backend.

1

There are 1 answers

2
Valerio8787 On BEST ANSWER

It depends on where you want to show that selector. The most common way is to expand template view data (you can use your entity repository to findAll() records. E.g. you can subscribe on Enlight_Controller_Action_PostDispatchSecure_Frontend_Account event if you wnat to show that selector in the customer account after that in the subscribed method you need to extend view data like

 public function onFrontendPostDispatchAssignViewData(\Enlight_Event_EventArgs $args)
{
    /** @var \Enlight_Controller_Action $controller */
    $controller = $args->get('subject');
    $view = $controller->View();
   
    $view->assign('productStreams', Shopware()->Container()->get('models')->getRepository(ProductStream::class)->findAll();
   
}

Now you should get productStreams in a template. You can extend the theme template now to show input, and probably you need to extend the save process to save the selected value into your new attribute.