faithcatholic / custom_metatags
Allows users to tweak metatags.
Package info
github.com/FaithCatholic/custom_metatags
Language:Shell
Type:drupal-module
pkg:composer/faithcatholic/custom_metatags
Requires
None
Requires (Dev)
- drupal/devel: *
- drupal/metatag: *
- drush/drush: *
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Gives a site one fallback image for Open Graph sharing. The module adds a
Metatag tag called "Default image", og:image:default, that you fill in once in
the Metatag defaults. On every non-admin page it then looks at the tags the page
is about to render. If the page has no og:image of its own, the default is
rewritten as og:image so social networks pick it up. If the page already has
one, the default is dropped, so a page never emits two og:image tags.
Note the naming: the repository directory and the Composer package are
custom_metatags, with an s, while the module machine name is custom_metatag,
without one. Use custom_metatag for drush en, namespaces and service ids.
Requirements
- Drupal 10.3 or newer, or Drupal 11.
- Metatag, with its Metatag: Open
Graph submodule (
metatag_open_graph) enabled. That submodule provides the Open Graph group the new tag joins, and the module will not install without it.
The module does nothing until the tag has a value. It expects a Metatag defaults
record, normally Global, with "Default image" filled in. It also waits for the
metatag_defaults entity type to exist before doing anything, so during an
upgrade it stays inert until database updates have run.
Installation
Install at /admin/extend, or run drush en custom_metatag.
If nothing changes after installing, the tag has not been set yet. See Usage.
Usage
- Go to /admin/config/search/metatag, edit a defaults record, usually Global, and set "Default image" in the Open Graph group. It takes the same token and image value as the regular Open Graph "Image" tag.
- Leave the regular "Image" tag empty at that level. If it has a value, every
page already has an
og:imageand the default is never used. - Per-page and per-bundle
og:imagevalues still win. The default only fills the gap. - Admin pages are skipped.
- There are no routes, no permissions and no settings form of its own. Editing the tag needs Metatag's "administer meta tags" permission.
Development
The repository carries a DDEV setup that builds a throwaway Drupal site around the module.
ddev startruns.ddev/post-start.sh, which fetches the dev dependencies with poser, installs the site if the database is empty, enables devel, metatag, metatag_open_graph and custom_metatag, and prints a one-time login link. The account isadminwith password1.- The module is symlinked into
web/modules/custom/custom_metatags. Bothweb/andvendor/are ignored by git. ddev phpcsandddev phpcbffor coding standards.ddev phpstan --level=6 -c ../../../../phpstan.neonfor static analysis. Pass the config explicitly. Plainddev phpstanruns without it and reports nothing useful, because the add-on symlinksphpstan.neoninto the module directory with a relative path that does not resolve, which silently drops the run to level 0 and skips the project ignores. The relative path above is correct because the wrapper changes into the module directory first.- Add-ons in use: ddev-drupal-contrib, ddev-drupal-contrib-extras, ddev-drupal and pimp-my-shell.
Notes
OgImageDefaultstill declares itself with the@MetatagTagannotation rather than a PHP attribute. Metatag 2.x still reads annotations, but they are being phased out and this needs converting before Drupal 12.custom_metatag_preprocess_html()incustom_metatag.moduleis a stub that exists only for Drupal 10, which cannot find hooks declared with attributes. It is marked#[LegacyHook], so Drupal 11 ignores it and runsCustomMetatagHooks::preprocessHtml()instead. Once the supported floor is Drupal 11.1 the stub, the .module file and the services.yml entry can all go, because Drupal 11 auto-registers classes insrc/Hook.