1

I have a form in which I am providing some options to users. But I want the options to appear in the form on the basis of presence of 3rd party libraries in vendor directory. I want something like this:

public function buildForm(array $form, FormStateInterface $form_state) { $form['settings']['tools'] = array( '#type' => 'radios', '#title' => $this->t('tools'), '#options' => array(), '#default_value' => $this->config('module.settings')->get('tools'), ); if(check_whether_xyz_library_is present_in_vendor_folder) { $form['settings']['tools']['#options'] = array('xyz' => 'xyz'); }

The vendor folder is present in the module directory itself and I am using composer to download third party dependencies. Since I do not want to check this during installation of module but during form rendering then I suppose hook_requirements is not option. Any ideas on how to do so?

3

If you are talking about libraries, which are defined in files like *.libraries.yml, then you can use this:

\Drupal::service('library.discovery')->getLibraryByName('MODULE_NAME', 'LIBRARY_NAME')

You'll receive an array with all data including paths to check that files are exist.

1

Since you mention that "the vendor folder is present in the module directory itself", you could check that a vendor file exists within your module:

$module_path = drupal_get_path('module', 'MY_MODULE');    
if (file_exists($module_path . '/js/MY_FILE.js')) {
  // ...
}

Another option would be to store the vendor library in the libraries folder, and check if the library exists with libraries_get_path():

$name = 'foo';    // your library name here.
if (function_exists('libraries_get_path') && file_exists(libraries_get_path($name))) {
  // ...
} 
  • Can u please tell me about libraries folder in Drupal 8? It would be great if u can provide me some idea regarding the appropriate place to keep vendor folder? – holdnet Jul 10 '15 at 17:23
  • Are you using the libraries module? If so, it allows you to store all your vendor libraries folders in the same place in Drupal, so you can share them across different modules if you need to. For example, you could store all your vendors libraries under sites/all/libraries directory. Libraries documentation – typologist Jul 10 '15 at 17:31
  • thanks, it seems library module for Drupal 8 is still in development phase!! – holdnet Jul 10 '15 at 19:55
  • So, use the first method in my answer. That should do the trick! – typologist Jul 10 '15 at 22:53
-1

The way external libraries are loaded and stored may change before (and even after) Drupal 8 release. Further sites build using composer may have the libraries installed in a different place than sites build with "manually" placed modules. Therefore I advise not to check the content of a vendor directory, but directly check for the presence of the library or one of its classes by using class_exists(). This way your code is independent of the library location.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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