<?php

add_filter('acf/fields/flexible_content/layout_title/name=page-blocks', 'codelibry_acf_flexible_layout_title_thumbnail', 10, 4);
add_filter('acf/fields/flexible_content/layout_title/name=reusable-blocks', 'codelibry_acf_flexible_layout_title_thumbnail', 10, 4);

function codelibry_acf_flexible_layout_title_thumbnail($title, $field, $layout, $i)
{
    $thumbnail = $layout['acfe_flexible_thumbnail'] ?? null;
    if (empty($thumbnail)) {
        return $title;
    }

    if (is_numeric($thumbnail)) {
        $thumbnail = wp_get_attachment_image_url((int) $thumbnail, 'medium');
    }

    if (empty($thumbnail)) {
        return $title;
    }

    return $title . sprintf(
        '<span class="codelibry-acf-layout-title-thumb"><img src="%s" alt=""></span>',
        esc_url($thumbnail)
    );
}
