many deprecated functions can be replaced by an equivalent call to the \Drupal class. rather than doing this manually and rerolling every time a patch breaks we should automate this and run the script at a time when the patch is likely to get committed. if we can make one big patch replacing lots of different functions then that will also prevent other issues needing multiple rerolls.

Comments

ianthomas_uk’s picture

Issue tags: +@deprecated
grom358’s picture

I have created a project to automate the replacement of deprecated functions, see the function_replace tool at https://github.com/grom358/d8codetools :)

ianthomas_uk’s picture

I've linked the language_* functions to this, although they may not be particularly suited as they are services that should really be injected (we still need to open an issue for language_default() too, or possibly shuffle all those issues around so they do all language_* functions and injection at the same time).

I think the other issues linked here are straight replacements. Some are so small that the script isn't really helpful, others have 100+ replacements needed.

We've got a good chance of getting at least some of these in over the next couple of days if we can get the patches rolled and reviewed. If you roll a patch with the function_replace tool please include the command line you used, so we can use that for the reroll.

Sutharsan’s picture

@grom358, great script!. I would like to use the script in #2223435: Do not use t() in block plugins anymore. Initially I made a bash script, but now I run into static functions where the replacement should not take place and your script handles this nicely t() calls should be replaced by $this->t() calls in a subset of files. Would it be feasible to modify the script to make the class and alias optional? I need to call the script for a subset of classes, located in .../Plugin/... directories.

grom358’s picture

@Sutharsan yes that is doable. Its built on Pharborist. The API is still a work in progress, but you can tell if the parent method is static by using $node->getParent() until you get to ClassMethodNode (if the call is inside a class method, otherwise the parent could be a FunctionDeclarationNode or even StatementBlockNode if its a top level statement). Then $method->modifiers->static !== NULL will tell you its a static method.

// Untested code to replace your t() calls to $this->t() calls
$tree = Parser::parseFile($filename);
$function_call_nodes = $tree->find('\Pharborist\FunctionCallNode');
foreach ($function_call_nodes as $function_call_node) {
  if ($function_call_node->functionReference instanceof NamespacePathNode && $function_call_node->functionReference->getChildCount() === 1) {
    $name = (string) $function_call_node->functionReference;
    if ($name === 't') {
      $pos = $name->getSourcePosition();
      $replacement = new TokenNode(T_STRING, '$this->t', $pos->lineNo, $pos->colNo); // @todo Fix hacky conversion
      $function_call_node->replaceChild($replacement, $function_call_node->functionReference);
    }
  }
}
ianthomas_uk’s picture

Status: Active » Closed (fixed)

This script is available at https://github.com/grom358/d8codetools