0

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' => 250,
      'height' => 300,
    ];
    $settings = [
      $form_id => [
        'title' => t('Successful Added'),
        'message' => t('Cart Updated Successfully'),
      ],
    ];
    $title = $settings[$form_id]['title'];
    $message = $_SESSION['messages']['status'][0]->__toString();
    if (!empty($this->cartBlock)) {
      // ****LINE TO REMOVE****
      $response->addCommand(new OpenModalDialogCommand($title, $message, $options));
      // **********************
    }
    else {
      $customblock = $this->container->get('plugin.manager.block')->createInstance('commerce_cart', []);
      $render = $customblock->build();
      // ****LINE TO REMOVE****
      $response->addCommand(new OpenModalDialogCommand($title, $render, $options));
      // **********************
    }
    $response->addCommand(new ReplaceCommand('.block-commerce-cart', $this->cartBlock));
    $response->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:

  1. 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)
  2. 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.

3
  • Without seeing the code of that file, it's hard to answer. It could be a class implementing a service, a event handler, or something else. Depending on what that class is, the answer changes, even in the case the class name is exposed from a hook.
    – avpaderno
    Commented Feb 2, 2018 at 13:42
  • @kiamlaluno i'll add the exact module & code snippets to the question. Thanks
    – cwiggo
    Commented Feb 2, 2018 at 13:43
  • @kiamlaluno i've updated the question
    – cwiggo
    Commented Feb 2, 2018 at 13:46

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.