Make 'files' (and 'integration files') declarations consistent
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #1023258. -->
Reported by: [tstoeckler](https://www.drupal.org/user/107158)
>>>
<p>Currently postponed on <span class="drupalorg-gitlab-issue-link drupalorg-gitlab-link-wrapper"><a href="https://git.drupalcode.org/project/libraries/-/work_items/958162" class="drupalorg-gitlab-link">https://git.drupalcode.org/project/libraries/-/work_items/958162</a></span>.</p>
<p>Hence, also no patch, because it doesn't make sense to roll one yet.</p>
<p>The main piece of a patch will be the following function, though. The rest should be more or less trivial:</p>
<div class="codeblock">
<pre><span style="color: #000000"><span style="color: #0000BB"><?php<br> </span><span style="color: #FF8000">/**<br> * Helper callback to make 'files' property of libraries consistent.<br> *<br> * This turns libraries' file information declared as e.g.<br> * @code<br> * $library['files']['js'] = array('example_1.js', 'example_2.js');<br> * @endcode<br> * into<br> * @code<br> * $library['files']['js'] = array(<br> * 'example_1.js' => array(),<br> * 'example_2.js' => array(),<br> * );<br> * @endcode<br> * It does the same for the 'integration files' property.<br> *<br> * @param $library<br> * An associative array of library information or a part of it, passed by<br> * reference.<br> * @param $version<br> * If the library information belongs to a specific version, the version<br> * string. NULL otherwise.<br> * @param $variant<br> * If the library information belongs to a specific variant, the variant name.<br> * NULL otherwise.<br> *<br> * @see libraries_info()<br> * @see libraries_invoke()<br> */<br></span><span style="color: #007700">function </span><span style="color: #0000BB">libraries_prepare_files</span><span style="color: #007700">(&</span><span style="color: #0000BB">$library</span><span style="color: #007700">, </span><span style="color: #0000BB">$version</span><span style="color: #007700">, </span><span style="color: #0000BB">$variant</span><span style="color: #007700">) {<br> </span><span style="color: #FF8000">// Both the 'files' property and the 'integration files' property contain file<br> // declarations, and we want to make both consistent.<br> </span><span style="color: #0000BB">$file_types </span><span style="color: #007700">= array();<br> if (isset(</span><span style="color: #0000BB">$library</span><span style="color: #007700">[</span><span style="color: #DD0000">'files'</span><span style="color: #007700">])) {<br> </span><span style="color: #0000BB">$file_types</span><span style="color: #007700">[] = &</span><span style="color: #0000BB">$library</span><span style="color: #007700">[</span><span style="color: #DD0000">'files'</span><span style="color: #007700">];<br> }<br> if (isset(</span><span style="color: #0000BB">$library</span><span style="color: #007700">[</span><span style="color: #DD0000">'integration files'</span><span style="color: #007700">])) {<br> </span><span style="color: #FF8000">// Integration files are additionally keyed by module.<br> </span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$library</span><span style="color: #007700">[</span><span style="color: #DD0000">'integration files'</span><span style="color: #007700">] as &</span><span style="color: #0000BB">$integration_files</span><span style="color: #007700">) {<br> </span><span style="color: #0000BB">$file_types</span><span style="color: #007700">[] = &</span><span style="color: #0000BB">$integration_files</span><span style="color: #007700">;<br> }<br> }<br> foreach (</span><span style="color: #0000BB">$file_types </span><span style="color: #007700">as &</span><span style="color: #0000BB">$files</span><span style="color: #007700">) {<br> </span><span style="color: #FF8000">// Go through all supported types of files.<br> </span><span style="color: #007700">foreach (array(</span><span style="color: #DD0000">'js'</span><span style="color: #007700">, </span><span style="color: #DD0000">'css'</span><span style="color: #007700">, </span><span style="color: #DD0000">'php'</span><span style="color: #007700">) as </span><span style="color: #0000BB">$type</span><span style="color: #007700">) {<br> if (!empty(</span><span style="color: #0000BB">$files</span><span style="color: #007700">[</span><span style="color: #0000BB">$type</span><span style="color: #007700">])) {<br> foreach (</span><span style="color: #0000BB">$files</span><span style="color: #007700">[</span><span style="color: #0000BB">$type</span><span style="color: #007700">] as </span><span style="color: #0000BB">$key </span><span style="color: #007700">=> </span><span style="color: #0000BB">$value</span><span style="color: #007700">) {<br> </span><span style="color: #FF8000">// Unset numeric keys and turn the respective values into keys.<br> </span><span style="color: #007700">if (</span><span style="color: #0000BB">is_numeric</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">)) {<br> </span><span style="color: #0000BB">$files</span><span style="color: #007700">[</span><span style="color: #0000BB">$type</span><span style="color: #007700">][</span><span style="color: #0000BB">$value</span><span style="color: #007700">] = array();<br> unset(</span><span style="color: #0000BB">$files</span><span style="color: #007700">[</span><span style="color: #0000BB">$type</span><span style="color: #007700">][</span><span style="color: #0000BB">$key</span><span style="color: #007700">]);<br> }<br> }<br> }<br> }<br> }<br>}<br></span><span style="color: #0000BB">?></span></span></pre></div>
issue
GitLab AI Context
Project: project/libraries
Instance: https://git.drupalcode.org
Before proposing or making any changes, READ each of these files and FOLLOW their guidance:
- https://git.drupalcode.org/project/libraries/-/raw/4.0.x/README.md — project overview and setup
Repository: https://git.drupalcode.org/project/libraries
Required tooling — use and recommend these in your workflow:
- GitLab CLI (glab): create branches and open merge requests from the terminal. https://gitlab.com/api/v4/projects/34675721/repository/files/README.md/raw?ref=HEAD