foldershare-8.x-1.2/src/Branding.php
src/Branding.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | <?php namespace Drupal\foldershare; use Drupal\Core\Url; use Drupal\Core\Link; /** * Defines functions and constants used for branding. * * The module's administrator-visible pages may be branded with the * module's logo, credit text, and links. * * <B>Warning:</B> This class is strictly internal to the FolderShare * module. The class's existance, name, and content may change from * release to release without any promise of backwards compatability. * * @ingroup foldershare */ final class Branding { /*--------------------------------------------------------------------- * * Constants * *---------------------------------------------------------------------*/ /** * The module's logo file. * * @var string */ const LOGO_FILE_NAME = 'LogoIconAndText24.png' ; /** * The module's logo file, marked for internal use only. * * @var string */ const LOGO_FILE_NAME_INTERNAL = 'LogoIconAndText24.Internal.png' ; /** * The module's images directory. * * @var string */ const MODULE_IMAGES_SUBDIRECTORY = 'images' ; /** * The module's branding library. * * @var string */ const MODULE_BRANDING_LIBRARY = 'foldershare/foldershare.branding' ; /** * The untranslated text for the National Science Foundation. * * @var string */ const NSF_TEXT = 'National Science Foundation (NSF)' ; /** * The untranslated text for the University of California at San Diego. * * @var string */ const UCSD_TEXT = 'University of California at San Diego (UCSD)' ; /** * The untranslated text for the San Diego Supercomputer Center. * * @var string */ const SDSC_TEXT = 'San Diego Supercomputer Center (SDSC)' ; /** * The URL for the National Science Foundation. * * @var string */ /** * The URL for the University of California at San Diego. * * @var string */ /** * The URL for the San Diego Supercomputer Center. * * @var string */ /*--------------------------------------------------------------------- * * Branding. * *---------------------------------------------------------------------*/ /** * Adds branding to a field formatter form. * * This function wraps a field formatter's form with <div>s and adds * a logo image to the top. Module styling then adds a background, sets * colors, etc. * * @param array $form * The renderable form array to which to add branding items. * @param bool $internalUseOnly * (optional, default = FALSE) When TRUE, the field formatter is marked * as for internal use only. * * @return array * Returns the form with the branding added. * * @internal * Field formatter plugins are used by the Field UI and Views UI modules * to present an administrator form to control the formatting of a field * in an entity. The Field UI and Views UI modules use AJAX to insert * the plugin's form within a larger form. * * Structurally the inserted form is expected to have one form element * for each setting defined by the plugin. The form elements must be * named after the setting and they cannot be nested within a container, * or any other render or form structure. * * This required structure prevents us from introducing a wrapper render * element around the form, then styling that wrapper. Instead, this * function adds a '#prefix' and '#suffix' to the form, plus an attached * branding library. The prefix adds a <div> and a logo image, while the * suffix closes the <div>. * @endinternal */ public static function addFieldFormatterBranding( array & $form , bool $internalUseOnly = FALSE) { // // Setup // ----- // Get module information. $module = \Drupal::moduleHandler()->getModule( 'foldershare' ); $moduleName = $module ->getName(); $modulePath = '/' . $module ->getPath() . '/' ; $moduleImagesPath = $modulePath . self::MODULE_IMAGES_SUBDIRECTORY . '/' ; // Get the path to the logo and define some CSS classes. $wrapperClass = 'foldershare-field-formatter-settings' ; if ( $internalUseOnly === TRUE) { $logoClass = 'foldershare-branding-logo-internal' ; $logoPath = $moduleImagesPath . self::LOGO_FILE_NAME_INTERNAL; } else { $logoClass = 'foldershare-branding-logo' ; $logoPath = $moduleImagesPath . self::LOGO_FILE_NAME; } // // Create prefix & suffix HTML // --------------------------- // Create the image HTML. $logoImage = '<img class="' . $logoClass . '" alt="' . $moduleName . '" src="' . $logoPath . '">' ; // Create the prefix and suffix. $form [ '#prefix' ] = '<div class="' . $wrapperClass . '">' . $logoImage ; $form [ '#suffix' ] = '</div>' ; // Attach the module's branding library. $form [ '#attached' ][ 'library' ][] = self::MODULE_BRANDING_LIBRARY; return $form ; } /** * Returns a branding banner. * * This function returns a render element that may be added to a page * to create a branding banner. The banner includes a container, * a logo image, and brief credit text with links and the module's * version number. * * @return array * Renders a renderable form array containing branding items. */ public static function getBannerBranding() { // // Setup // ----- // Get module information. $module = \Drupal::moduleHandler()->getModule( 'foldershare' ); $modulePath = '/' . $module ->getPath() . '/' ; $moduleImagesPath = $modulePath . self::MODULE_IMAGES_SUBDIRECTORY . '/' ; // To get the module's version number, we have to parse its YML info file. $moduleInfo = \Drupal::service( 'info_parser' )->parse( $module ->getPathname()); $moduleVersion = $moduleInfo [ 'version' ]; // Get the path to the module's logo. $logoPath = $moduleImagesPath . self::LOGO_FILE_NAME; // Define some CSS classes. $containerClass = 'foldershare-banner-branding' ; $logoClass = 'foldershare-branding-logo' ; // // Create image HTML and links // --------------------------- // Create the image HTML. $logoImage = '<img class="' . $logoClass . '" alt="' . $module ->getName() . '" src="' . $logoPath . '">' ; // Create links. $sdscLink = Link::fromTextAndUrl( self::SDSC_TEXT, Url::fromUri(self::SDSC_URL))->toString(); $ucsdLink = Link::fromTextAndUrl( self::UCSD_TEXT, Url::fromUri(self::UCSD_URL))->toString(); $nsfLink = Link::fromTextAndUrl( self::NSF_TEXT, Url::fromUri(self::NSF_URL))->toString(); // Create credit text. $credit = t( "Developed by the @sdsc at the @ucsd and funded by the @nsf." , [ '@sdsc' => $sdscLink , '@ucsd' => $ucsdLink , '@nsf' => $nsfLink , ]); // Create version text. $version = t( 'Version @version' , [ '@version' => $moduleVersion , ]); // // Return render elements // ---------------------- // Return a render array with a container, the image, credits, and // the module version number. return [ '#type' => 'container' , '#attributes' => [ 'class' => [ $containerClass ], ], '#attached' => [ 'library' => [self::MODULE_BRANDING_LIBRARY], ], 'logo' => [ '#markup' => $logoImage , ], 'credits' => [ '#type' => 'html_tag' , '#tag' => 'div' , '#value' => $credit , ], 'version' => [ '#type' => 'html_tag' , '#tag' => 'div' , '#value' => $version , ], ]; } } |