Load a random entity / Create a random number

Created on 20 March 2024, 9 months ago
Updated 20 June 2024, 6 months ago

Problem/Motivation

Do you have an action of generating a random integer between a number range? I need to include a random product info in email communication to users. If I can get a random number for the pid, I can load the entity of pid to get the product informaiton.

if you don't have this feature, could you advise how to load a random product entity?

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

💬 Support request
Status

Closed: works as designed

Version

1.1

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @sdsc
  • 🇩🇪Germany jurgenhaas Gottmadingen

    AFAIK there is no action plugin available yet to generate randoms. But you could use the Views: Execute query action to receive a list of entities with a random sort in the view. When you then take the first one from the results, that should be a random entity.

  • Thank you! What is the command of taking the first one form the result? If there are multiple fields, how to retrieve a certain field? For example, the view result is stored in "get_ad". When I display the message of "get_ad", it prints everything of that record, rather than the field I setup in the view.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    If there are multiple fields, how to retrieve a certain field?

    The views query always returns a list of entities. Your field configuration in a view does not have any impact, they are only used later when rendering the output. But for the query, it's only always returning entities. You can then use actions like Entity: get field value to retrieve field values.

    What is the action of taking the first one from the result?

    You can use the List: remove item action for that.

    Or, you can configure the pager of your view to output only 1 item.

  • My query just displays one item and one field.
    I use VIEWS: EXECUTE QUERY to load the view, and the name of token is "get_htad", I then display the message of [get_htad], it gives me everything of that product from product_id to format. The field I need to retrieve is "ht_ad".

    I used [get_htad:ht_ad], [get_htad:ht_ad:value], [ht_ad], nothing came out.

    I used "get field value" as shown in the attached image. Then I tried to print [this_ad], nothing came out.

    Not sure what I did wrong.

  • I got it!! I need to use List: remove item before getting the data. This ticket can be closed.

    Thank you!

  • Status changed to Closed: works as designed 9 months ago
  • 🇳🇿New Zealand roxflame

    For anyone else looking to use random numbers you can leverage the flexibility of twig

    https://twig.symfony.com/doc/2.x/functions/random.html

    Use the "Render: Twig" action with {{ random("min", "max") }}
    (replace min and max with range you want, you can include tokens)

    You can also get random items from lists
    {{ random([your-list]) }}

    {{ random(['apple', 'orange', 'citrus']) }} {# example output: orange #}

  • 🇳🇿New Zealand roxflame

    I'll also note, that twig here is super useful for manipulating the rendering of lists in ECA in general without having to make complex loops

    {% for item in my_array %}
        <div>{{ item }}</div>
    {% endfor %}
    

    Or grab your random item and wrap it in the html you might want in the same twig action, etc

    <div>{{ random([your-list]) }}</div>

Production build 0.71.5 2024