Problem/Motivation

Yes, Drush, I know. Still, there are situations when we can't use Drush (eg. in a Docker dev env if set up that way, or just any other reason). It would be so nice to have a links.menu.yml and routing.yml entry so that it can go into the usual (Adminimal or stock) toolbar for easy call from the UI. Pretty, please... :-)

Comments

djg_tram created an issue. See original summary.

plach’s picture

Patches welcome :)

djg_tram’s picture

Issue summary: View changes
djg_tram’s picture

I knew it. :-) The problem is that my site I want to try it own doesn't run now, because of that problem that the module is supposed to fix, and I don't have Drush because I couldn't yet set it up in Docker. :-)

I can't immediately see how the actual operation could be called without Drush being present. Is it possible at all? Because the outer shell would be relatively simple:

devel_entity_updates.routing.yml

devel_entity_updates.menu_entity_update:
  path: '/devel/menu/entity-update'
  defaults:
    _form: '\Drupal\devel_entity_updates\Form\EntityUpdateConfirmForm'
    _title: 'Update entities'
  options:
    _admin_route: TRUE
  requirements:
    _permission: 'administer site configuration'

devel_entity_updates.links.menu.yml

devel_entity_updates.menu_entity_update:
  title: 'Update entities'
  route_name: devel_entity_updates.menu_entity_update
  menu_name: devel
  class: \Drupal\devel\Plugin\Menu\DestinationMenuLink

src/Form/EntityUpdateConfirmForm.php

<?php

namespace Drupal\devel_entity_updates\Form;

use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteBuilderInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\devel_entity_updates\DevelEntityDefinitionUpdateManager;

/**
 * Provides confirmation form for rebuilding the routes.
 */
class EntityUpdateConfirmForm extends ConfirmFormBase {
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'devel_entity_updates';
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this->t('Are you sure you want to update the entities?');
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return new Url('<front>');
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this->t('Apply schema updates while developing new entity types and ending up incrementally adding/removing/changing entity type/field definitions.');
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this->t('Update');
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    \Drupal::classResolver()
      ->getInstanceFromDefinition(DevelEntityDefinitionUpdateManager::class)
      ->applyUpdates();
    $this->messenger()->addMessage($this->t('The entities have been updated.'));
    $form_state->setRedirect('<front>');
  }

}

With the //... being the interesting part...

plach’s picture

The interesting part should just be DevelEntityDefinitionUpdateManager::applyUpdates() :)

plach’s picture

djg_tram’s picture

Certainly, but that's not static to call easily, we would need to round up the injections normally provided by Drush, I suppose, to construct that command and then be able to call its member function.

plach’s picture

This should do the trick:

\Drupal::classResolver()
  ->getInstanceFromDefinition(DevelEntityDefinitionUpdateManager::class)
  ->applyUpdates();

Of course we should inject the class resolver.

djg_tram’s picture

Yes, I arrived at that just at the same time. My only problem is that while it runs, it doesn't cure my site. :-)) Much ado about nothing, but only in my particular case, of course...

djg_tram’s picture

But I changed the source above, anyway, to be functional. :-)

djg_tram’s picture

OK, I cured the site with some manual editing of the key_value table. Of course, I didn't have just the right backup right now. :-))

So, what I'm stuck with is adding the item to the Devel menu in the toolbar. It seems this depends on Devel itself and additional modules can't have a say in it. So while it works by manually typing the route, the last step is still missing.

djg_tram’s picture

Sorry, found the culprit. The Admin Toolbar Tools module ignores what Devel sets up and provides its own hardwired menu in the toolbar. It's their fault, I'll go there to complain.

So, if you can use the small code above, please, do.