Overriding third-party module code I am using Composer to download third-party modules to my Drupal 8 repository.

I have a requirement that involves all of the functionality that module gives but there are unfortunately 2 lines of code that I need to remove in a PHP file in the module_name/src/Helper folder.

The lines I've commented out are to stop the request to open a jQuery UI dialog box.

I'm curious as to what you guys think. It is a requirement from the client that I have this functionality in place.

Here is the code snippet taken from the Ajax Add To Cart Module:

namespace Drupal\ajax_add_to_cart\Helper;

use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\ajax_add_to_cart\Ajax\ReloadCommand;
use Drupal\block\Entity\Block;
use Drupal\Core\Ajax\ReplaceCommand;

/**
 * Class AjaxCartHelper.
 *
 * @package Drupal\modules\ajax_add_to_cart
 */
class AjaxCartHelper {

  /**
   * Ajax add to cart response.
   *
   * @param string $form_id
   *   Form id.
   * @param object $response
   *   Response object to store information.
   *
   * @return object
   *   Return response object.
   */
  public function ajaxAddToCartAjaxResponse($form_id, $response) {
    // Adding modal window.
    $options = [
      'width' =&gt; 250,
      'height' =&gt; 300,
    ];
    $settings = [
      $form_id =&gt; [
        'title' =&gt; t('Successful Added'),
        'message' =&gt; t('Cart Updated Successfully'),
      ],
    ];
    $title = $settings[$form_id]['title'];
    $message = $_SESSION['messages']['status'][0]-&gt;__toString();
    if (!empty($this-&gt;cartBlock)) {
      // ****LINE TO REMOVE****
      $response-&gt;addCommand(new OpenModalDialogCommand($title, $message, $options));
      // **********************
    }
    else {
      $customblock = $this-&gt;container-&gt;get('plugin.manager.block')-&gt;createInstance('commerce_cart', []);
      $render = $customblock-&gt;build();
      // ****LINE TO REMOVE****
      $response-&gt;addCommand(new OpenModalDialogCommand($title, $render, $options));
      // **********************
    }
    $response-&gt;addCommand(new ReplaceCommand('.block-commerce-cart', $this-&gt;cartBlock));
    $response-&gt;addCommand(new ReloadCommand());
    unset($_SESSION['messages']);
    return $response;
      }
}


I've added the comment - / ****LINE TO REMOVE**** to the lines that need to be removed

My ideas are:


Duplicate the whole module so that it's custom and use that as the solution (even though module updates won't affect the custom module and i'll have to update it each time)
Comment the two lines out and then update the third-party module 


Is there any other methods I'm missing? I've seen the libraries override but thats for the purposes of theming purposes- JS and CSS, I just wondered if theres anything out there in the drupal community fro overriding the PHP that modules provide?

It must be a common requirement by Drupal Devs.
I think the best method would be to use the cweagan patch manager package with composer and a local patch file.  This would allow the module to be updated normally with composer and the patch applied. If the code changes too much for the patch, it will fail and you can update the patch.

Just require cweagans/composer-patches and then modify your composer.json to include the patch.

Also, this is a good way to add patches from Drupal.org or github issues.  If your fix is something that should be generally available, sumbit an issue with the patch.  Then set up your composer patch section to use the URL to the patch in the issue.  For d.o. patches, this makes it easy to find the issue related to the patch since it issue number is in the url.