Drupal StackExchange

How can I programmatically create a media entity?

3 days 2 hours ago

I'm trying to programmatically create a media file. Here's what I got so far:

$file_data = file_get_contents('http://www.africau.edu/images/default/sample.pdf'); $file = file_save_data($file_data, 'public://3010.pdf', FILE_EXISTS_REPLACE); $media = Media::create([ 'bundle' => 'file', 'uid' => '0', 'field_media_file' => [ 'target_id' => $file->id(), ], ]); $media->setName('Hello') ->setPublished(TRUE) ->save();

Unfortunately this returns me the following error and the media file doesn't get saved:

Error: Call to a member function getSource() on null in Drupal\media\Entity\Media->getSource() (line 138 of core/modules/media/src/Entity/Media.php).

What am I doing wrong? How can I fix this error?

ravi teja

How to trigger email by comparing date value in node?

3 days 2 hours ago

I'm using Drupal 7, with Rules and Rules Scheduler.

I have a content type "Tutorial Class", with fields as below:

  • Class Date and Time
  • Feedback requested (Radio button: Yes/No, default is no)

I would like to create a Rule, so that whenever the cron runs, it will check on all "Tutorial Class" content type.

If current time is larger than "Class Date and Time" field, and "Feedback requested" is no, an email will be triggered and "Feedback requested" will be changed to Yes.

Currently I know how to trigger email after "Tutorial Class" is created, but I can't get the above Rule done.

Any idea of how to set this rule?

vences

How to modify a paragraph behavior setting programmatically?

3 days 3 hours ago

I would like to alter/change a certain paragraph behavior plugin setting.

If I debug the paragraph entity, I can find the behavior_settings value, but it's json encoded and I don't think the best option is to decode it, change the values, encode it again and then assign it back to the paragraph, there must be a much better way.

Alejandro

Change default format for date field on user register form

3 days 3 hours ago

I'm new to Drupal 8. In the user registration form, i've added a Date field via manage fields in user account settings. However, I need to customize this field and change its format. I tried 'manage display' but it's not taking effect. Is there something I'm missing. (Ideally, it should look and behave just like field datetime in Drupal 8 webform)

Appreciate any help given.

Thanks

ram_griever

How do I get the price of a line item for the transactions which are already completed?

3 days 4 hours ago

There are some transactions which are made using drupal commerce module along with commerce ccavenue module. I have the order ids, transaction ids and list of line items purchased in each of the orders. But the problem is I if a product price is updated in past then I am not able to find the cost of the product in the past order when the product was purchased.

Is there any place where drupal stores product price for a particular order anywhere.

user24595

How to rebuild images using Imagemagick?

3 days 5 hours ago

I have switched the Image toolkit from GD2 to Imagemagick. It’s working great for new images, however previously uploaded images still show up with lowres quality.

How can I regenerate ALL images using Imagemagick?

I've followed up: Rebuild images from image style

  • drush: drush image-flush mystyle
  • rm -rf sites/default/files/styles
  • Clearing Caches a couple of times and hit save on all image styles.

However all images still show up with lowres quality. When I manually update a node, the image quality looks correct. Also, the file size changes. Looks like imagemagick isn’t triggered. What can I do?

user32010

Drupal Planet

DrupalEasy: cspell and drupalorg CLI: two useful tools for Drupal contrib module maintainers

3 days 5 hours ago

While preparing the recent Markdown Easy 1.0.1 release, I utilized a couple of tools that I hadn't used before in order to help improve code quality as well as the quality of the release notes.

cspell

cspell is a Node.js spell-checker for code that was made available to the default Drupal GitLab templates in February, 2024. There is a new SKIP_CSPELL variable that can be set if you'd like your project to completely ignore cspell (it is enabled by default.) Documentation on using cspell in Drupal GitLab pipelines is available. I'd wager that most Drupal contrib maintainers will have the need for a custom word list (as I did,) so I took a few minutes to learn a bit more about it.

Cspell uses its default wordlist along with a few add-on dictionaries related to Drupal development (anecdotally, words like "mglaman," "gloop," "skynet," and "vampirize" are included) for checking the spelling of both variable names as well as code comments, but luckily, additional words (and patterns) can be added on a per-project basis in order to achieve a clean cspell report in GitLab pipelines. While there are several methods to add a list of custom words, the way I felt was most elegant (and easy for me to remember in the future!) was to add a .cspell-project-words.txt file on the project root (in this case, the contrib module is the project.) This file then includes a list of words that cspell should not flag as misspellings. For the Markdown Easy project, I went with "Anello," "~commonmark," and "~ultimike" (possibly for obvious reasons.) The ~ modifier indicates those words are case-insensitive (additional modifiers are available.) With that change, Markdown Easy now earns a passing score from the cspell pipeline. 

drupalorg CLI

drupalorg is a command-line interface maintained by (who else?) Matt Glaman. I actually learned about this tool a few years ago after Matt blogged about it, but for whatever reason, I only thought about installing and (finally) using it recently. The general idea is to provide a command line tool for contributors to interact with drupal.org. It's available commands are:

help Displays help for a command list Lists commands cache cache:clear (cc) Clears caches ci drupalci drupalci:list (ci:l) Lists test results for an issue drupalci:watch (ci:w) Watches a Drupal CI job issue issue:apply Applies the latest patch from an issue. issue:branch Creates a branch for the issue. issue:interdiff Generate an interdiff for the issue from local changes. issue:link Opens an issue issue:patch Generate a patch for the issue from committed local changes. maintainer maintainer:issues (mi) Lists issues for a user, based on maintainer. maintainer:release-notes (rn, mrn) Generate release notes. project project:issues (pi) Lists issues for a project. project:kanban Opens project kanban project:link Opens project page project:release-notes (prn) View release notes for a release project:releases Lists available releases tci travisci travisci:list (tci:l) Lists Travis Ci builds for a Drupal project travisci:watch (tci:w) Watches a Travis CI job

The maintainer:release-notes command is especially helpful to automatically generate release notes for a new version of a Drupal contrib project. Here's how I installed and used this tool to generate the release notes for Markdown Easy 1.0.1:

  1. I downloaded the drupalorg.phar to a new ~/sites/drupalorg/ directory on my local, and renamed the file to just drupalorg.
  2. I then gave execute permission to this file via chmod u+x ~/sites/drupalorg/drupalorg
  3. I use zsh, so I added the following to my ~/.zshrc file and then restarted my terminal:

    # drupalorg command line tool export PATH="/Users/michael/sites/drupalorg:$PATH"
  4. Here's the important part - I then navigated to my local, working copy of the Markdown Easy module. In my case cd ~/sites/d10/web/modules/contrib/markdown_easy 
  5. I then ran the following command to generate the release notes: drupalorg maintainer:release-notes 1.0.0  

I assumed at first that I should use the current release (1.0.1) as the argument, but after reading the documentation, I discovered that the command will generate release notes from the tag provided in the argument.

I also originally assumed that the command would take a contrib module's machine name as an argument, but after a few minutes of poking around the documentation, I realized that the command must be run from the module's directory.

You can see the automatically generated release notes here.

Summary

Both of these tools were rather easy to implement (once I read just a little bit of documentation) and seem like they'll be useful for just about any Drupal contrib maintainer. 

qtatech.com blog: Drupal 9 to 10 Transition Made Simple: Real Code Insights

3 days 8 hours ago
Drupal 9 to 10 Transition Made Simple: Real Code Insights kanapatrick Tue, 04/23/2024 - 10:31

Have you found yourself gearing up for the transition from Drupal 9 to Drupal 10, only to be met with complexities and uncertainties along the way? You're not alone in this journey. Making the switch between major versions of a CMS can feel like navigating through a maze of code and updates, leaving you puzzled and overwhelmed.