schemadotorg_demo-1.0.x-dev/modules/schemadotorg_demo_standard/schemadotorg_demo_standard.module
modules/schemadotorg_demo_standard/schemadotorg_demo_standard.module
<?php
/**
* @file
* Provides an opinionated demo built on top of Drupal's standard profile.
*/
declare(strict_types=1);
use Drupal\Core\Form\FormStateInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\views\ViewExecutable;
/**
* Implements hook_page_attachments().
*/
function schemadotorg_demo_standard_page_attachments(array &$page): void {
// Attach CSS and JavaScript, that fixes minor UI issues with
// minor UX enhancements.
$page['#attached']['library'][] = 'schemadotorg_demo_standard/schemadotorg_demo_standard';
}
/**
* Implements hook_schemadotorg_mapping_defaults().
*/
function schemadotorg_demo_standard_schemadotorg_mapping_defaults_alter(array &$defaults, string $entity_type_id, ?string $bundle, string $schema_type): void {
// Handle the unique case where the standard article content type is
// still using the image field.
if ($entity_type_id === 'node'
&& $bundle === 'article'
&& $schema_type === 'Article'
&& empty($defaults['properties']['image']['name'])
&& FieldStorageConfig::loadByName('node', 'field_image')
) {
$defaults['properties']['image']['name'] = 'field_image';
}
}
/* ************************************************************************** */
// Entity print enhancements.
/* ************************************************************************** */
/**
* Implements hook_link_alter().
*/
function schemadotorg_demo_standard_link_alter(array &$variables): void {
if (!isset($variables['url'])) {
return;
}
/** @var \Drupal\Core\Url $url */
$url = $variables['url'];
if ($url->isRouted() && $url->getRouteName() === 'entity_print.view') {
// Style entity print links as buttons.
$variables['options']['attributes']['class'] = array_merge(
$variables['options']['attributes']['class'],
['button', 'button--small', 'button--extrasmall']
);
}
}
/* ************************************************************************** */
// Entity browser enhancements.
/* ************************************************************************** */
/**
* Implements hook_form_FORM_ID_alter().
*
* @see content_browser_form_alter()
*/
function schemadotorg_demo_standard_form_views_exposed_form_alter(array &$form, FormStateInterface &$form_state): void {
// Remove types that are contextually disallowed by the View.
if (isset($form['#id']) && str_starts_with($form['#id'], 'views-exposed-form-node-browser-entity-browser')) {
/** @var \Drupal\views\ViewExecutable $view */
$view = $form_state->get('view');
if ($view instanceof ViewExecutable && isset($view->argument['type'])) {
/** @var \Drupal\node\Plugin\views\argument\Type $type */
$type = $view->argument['type'];
$value = $type->getValue();
if (!is_null($value) && !$type->isException($value)) {
$types = explode('+', $value);
$types[] = 'All';
foreach ($form['type']['#options'] as $name => $option) {
if (!in_array($name, $types, TRUE)) {
unset($form['type']['#options'][$name]);
}
}
}
}
}
}
