Domanda

We have a custom module, mymodule, that we are using to create a custom block programmatically. We tried to do the following and it does enable the module successfully, but i get the following warning.

What is the argument that needs to be passed in?

Missing argument 1 for mymodule_update_8501()

mymodule.install

function mymodule_install() {
  // Run update on install. 
  mymodule_update_8501();
}

function mymodule_update_8501(&$sandbox) {
  // Do something.
}
È stato utile?

Soluzione

A hook_update_N implementation is generally supposed to be invoked by the system, not manually.

Instead of thinking of it as "run an update on install", you could think of it as "perform an action on both install and update". A fairly common pattern for that is:

function MYMODULE_install() {
  _MYMODULE_install_something();
}

function MYMODULE_update_8501(&$sandbox) {
  _MYMODULE_install_something();
}

function _MYMODULE_install_something() {
  // Shared functionality here.
}

You don't have to use a global function; you could implement the shared functionality in a service and make use of that instead. The point is to separate out the logic for what needs to be done, and consume it in the same way wherever you need to.

If you do want to continue with your current approach, just pass it any initialised variable - doesn't matter what it is if you're not using it, and if your code is the only caller, it won't have any side-effects.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a drupal.stackexchange
scroll top