- Issue created by @jnicola
- πΊπΈUnited States jnicola
Attached a patch that works for us. I recognize the better way to do it is a commit in a fork but... eh I couldn't be bothered and since nobody else seems to be having this problem it likely doesn't matter.
Solution though was simple. Instead of assuming the key of 0 exists, just iterative over keys until you find the first numerical key, and run with that if you get it. Further improvements are likely possible, but this works and doesn't feel hacky to me.
- πΊπΈUnited States jnicola
Uploading patch with slight tweak after code review. Original code returned based on first item value, so now we just return on the first found media item versus assuming the first item is 0.
Thank you for the patch! Definitely an improvement that should be merged in. Just one note: Shouldn't the check whether actually processing inserting media be kept to prevent potential interference with other hooks, and abort processing the hook early if not relevant for the current operation? Just adding an early
return
:function insert_media_insert_variables($insertType, array &$element, $styleName, &$vars) { if ($insertType !== INSERT_TYPE_MEDIA) { return; } foreach ($element['selection'] as $key => $val) { if (is_int($key)) { $vars['media_entity'] = $element['selection'][$key]['rendered_entity']['#media']; $mid = !is_null($vars['media_entity']) ? $vars['media_entity']->id() : 0; $element['#name'] = 'media_ ' . $mid; return; } } }