Allow tool execute to be previewed
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3558037. -->
Reported by: [michaellander](https://www.drupal.org/user/636494)
>>>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p>It's been requested that we allow tools to be 'previewed'. This would mostly only affect tools that write out a permanent change. This is helpful in UI's when you want to simulate a flow, or debug, and this may be helpful in AI to allow more advanced 'planning' strategies.</p>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<p>Implement a preview method:</p>
<div class="codeblock">
<pre><span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #007700">public function </span><span style="color: #0000BB">preview</span><span style="color: #007700">() {<br> </span><span style="color: #0000BB">$tool</span><span style="color: #007700">-></span><span style="color: #0000BB">setPreview</span><span style="color: #007700">();<br> </span><span style="color: #0000BB">$tool</span><span style="color: #007700">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br> </span><span style="color: #0000BB">$tool</span><span style="color: #007700">-></span><span style="color: #0000BB">setPreview</span><span style="color: #007700">(</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">);<br>}<br><br></span><span style="color: #0000BB">$tool</span><span style="color: #007700">-></span><span style="color: #0000BB">preview</span><span style="color: #007700">();<br></span><span style="color: #0000BB">?></span></span></pre></div>
<p>And then inside of write tool <code>execute()</code>, include <code>isPreview()</code> check..</p>
<p>For example:</p>
<div class="codeblock">
<pre><span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #007700">if (!</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">isPreview</span><span style="color: #007700">()) {<br> </span><span style="color: #0000BB">entity</span><span style="color: #007700">-></span><span style="color: #0000BB">save</span><span style="color: #007700">();<br>}<br></span><span style="color: #0000BB">?></span></span></pre></div>
<h3 id="summary-remaining-tasks">Remaining tasks</h3>
<ul>
<li>Introduce <code>PreviewExecutableInterface</code></li>
<li>Add <code>PreviewExecutableTrait</code></li>
</ul>
issue