smart_trim-8.x-1.3/smart_trim.module
smart_trim.module
<?php /** * @file * Nothing to see here. It's all about the Field/FieldFormatter. */ use Drupal\Core\Routing\RouteMatchInterface; /** * Implements hook_help(). */ function smart_trim_help($route_name, RouteMatchInterface $route_match): string { $output = ''; switch ($route_name) { case 'help.page.smart_trim': $output = '<h3>' . t('About') . '</h3>'; $output .= '<p>' . t('Smart Trim implements a new field formatter for text fields (text, text_long, and text_with_summary, if you want to get technical) that improves upon the "Summary or Trimmed" formatter.') . '</p>'; } return $output; } /** * Implements hook_theme(). */ function smart_trim_theme(array $existing, string $type, string $theme, string $path): array { return [ 'smart_trim' => [ 'variables' => [ 'output' => NULL, 'is_trimmed' => NULL, 'wrap_output' => NULL, 'wrapper_class' => NULL, 'more' => NULL, 'more_wrapper_class' => NULL, 'field' => NULL, 'entity_type' => NULL, 'entity_bundle' => NULL, ], ], ]; } /** * Implements hook_theme_suggestions_alter(). */ function smart_trim_theme_suggestions_smart_trim_alter(array &$suggestions, array $variables) { $values = []; // Add all variables to array. $values[] = $variables['entity_type'] ?? NULL; $values[] = $variables['entity_bundle'] ?? NULL; $values[] = $variables['field'] ?? NULL; // Remove any missing values. $values = array_filter($values); $results = [[]]; // Loop through all values to form all combinations. foreach ($values as $element) { foreach ($results as $combination) { array_push($results, array_merge($combination, [$element])); } } // Sort by length so most specific suggestions come last. usort($results, fn($a, $b) => count($a) - count($b)); // Add all discovered combinations to suggestions. $suggestions += array_map(fn($array) => 'smart_trim__' . implode('__', $array), array_filter($results)); }