Custom Fields
Re-Envisioned
Support MB Views Adding If statement inside a loop in views

  • Creator
    Topic
  • #31504
    Kirb
    Participant

    Hi, Metabox. Can you help me how can I make this work? How can I add if statement inside the for loop? I don't know why this is not working. So basically I add the fields inside the group "upload_documents". Now, I want to check if the "project_documents" is empty or not. Thanks in advance.

    {% for clone in post.upload_documents %}
        {% for item in clone.project_documents %}
            {% for item in clone.project_documents %}
                {% if item.project_documents is empty %}
                <h3>Its empty</h3>
                {% else %}
                <h3>Its not empty</h3>
                {% endif %}
            {% endfor %}
        {% endfor %}
    {% endfor %}
Viewing 5 replies - 1 through 5 (of 5 total)
  • Author
    Replies
  • #31516
    Long Nguyen
    Moderator

    Hi Kirb,

    I see you have two for loops that get project_documents, you can try to remove one and re-check the result. On another hand, you can share the code that creates your custom fields, I will help you to correct the loop.

    #31517
    Kirb
    Participant

    Hi, Long. Thanks for the reply.

    I also tried that but still doesn't work. So basically the upload_documents is a group id for the first loop and the fields inside it are the project_documents. What I want to achieve is to see, if data of project_documents is empty then do something if not then do this. Thanks in advance.

    Here's my custom fields setup:

    <?php
    add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
    
    function your_prefix_function_name( $meta_boxes ) {
        $prefix = '';
    
        $meta_boxes[] = [
            'title'      => __( 'Project Details', 'your-text-domain' ),
            'id'         => 'project-details',
            'post_types' => ['project'],
            'geo'        => [
                'api_key' => 'AIzaSyCxo8eTReDw2Ehp9zYovWw4W9VRwEsD8Cs',
            ],
            'fields'     => [
                [
                    'id'   => $prefix . 'custom_html_2avzqnhkuwl',
                    'type' => 'custom_html',
                    'std'  => 'To upload images in this project, please go to the right sidebar and find the Project Image.',
                ],
                [
                    'name'              => __( 'Project Documents', 'your-text-domain' ),
                    'id'                => $prefix . 'upload_documents',
                    'type'              => 'group',
                    'collapsible'       => true,
                    'save_state'        => true,
                    'group_title'       => '{project_documents}',
                    'clone'             => true,
                    'sort_clone'        => true,
                    'clone_as_multiple' => true,
                    'add_button'        => __( 'Upload more', 'your-text-domain' ),
                    'before'            => __( 'מסמכי הפרויקט', 'your-text-domain' ),
                    'fields'            => [
                        [
                            'name' => __( 'Add Thumbnail Icon', 'your-text-domain' ),
                            'id'   => $prefix . 'document_icon',
                            'type' => 'single_image',
                        ],
                        [
                            'name'             => __( 'Add Document', 'your-text-domain' ),
                            'id'               => $prefix . 'project_documents',
                            'type'             => 'file_advanced',
                            'max_file_uploads' => 1,
                        ],
                    ],
                ],
                [
                    'name'   => __( 'Project Address', 'your-text-domain' ),
                    'id'     => $prefix . 'project_address_bar',
                    'type'   => 'text',
                    'before' => __( 'כתובת הפרויקט', 'your-text-domain' ),
                ],
                [
                    'name'   => __( 'Project Map', 'your-text-domain' ),
                    'id'     => $prefix . 'project_map_group',
                    'type'   => 'group',
                    'before' => __( 'מפת הפרויקט', 'your-text-domain' ),
                    'fields' => [
                        [
                            'name'   => __( 'Address Name', 'your-text-domain' ),
                            'id'     => $prefix . 'address_name',
                            'type'   => 'text',
                            'before' => __( 'כתובת', 'your-text-domain' ),
                        ],
                        [
                            'name'          => __( 'Map', 'your-text-domain' ),
                            'id'            => $prefix . 'osm_map',
                            'type'          => 'osm',
                            'address_field' => 'address_name',
                            'language'      => 'en',
                            'region'        => 'il',
                            'before'        => __( 'מַפָּה', 'your-text-domain' ),
                        ],
                    ],
                ],
            ],
        ];
    
        return $meta_boxes;
    }
    #31525
    Long Nguyen
    Moderator

    Hi,

    If a clone of a group or a field is not added value, it will not save any data to the database so you needn't check the value of the file uploaded existed. Just show them

    {% for clone in post.upload_documents %}
        {% for item in clone.project_documents %}
            {{ item.url }} <br>
        {% endfor %}
    {% endfor %}
    #31532
    Kirb
    Participant

    Hi, I don't have problems showing the item, my problem is how can I put an if statement inside a loop or condition if it is empty.

    {% for clone in post.upload_documents %}
        {% for item in clone.project_documents %}
            {% if item.url is empty %}
            <h3>empty</h3>
            {% else %}
            <h3>{{ item.url }}</h3>
            {% endif %}
        {% endfor %}
    {% endfor %} 
    #31541
    Long Nguyen
    Moderator

    Hi,

    You can check if the field project_documents is empty if you add the value to the field document_icon before. If two fields are empty, the clone group will not be saved and just needn't check value exist.

    {% for clone in post.upload_documents %}
        {% if( clone.project_documents is empty ) %}
            Empty!!!
        {% endif %}
    
        {% for item in clone.project_documents %}
            {{ item.url }} <br>
        {% endfor %}
    {% endfor %}
Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.