Veröffentlicht von md am 5. März 2010 - 12:30
Um so ein gestyltes Download Element, wie in der Abbildung zu sehen, hier im Drupal Magazin zu erzeugen, müssen wir immer viel zu viel tippen. Insbesondere der Link zur entsprechenden Drupal Projektseite nervt.
Also habe ich ein kleines Modul geschrieben, dass uns diese Arbeit erleichtert. Um die Box mit einem Link zu erzeugen, reicht es jetzt [views#Views herunterladen]zu schreiben. Und da wir auch oft Drupal und mdwp* hier eintippen, habe ich das auch in das Modul integriert.
Es ist ein sehr einfaches Filter Modul. Hier der Code:
function filter_example_filter($op, $delta = 0, $format = -1, $text = '') {
// The "list" operation provides the module an opportunity to declare both how
// many filters it defines and a human-readable name for each filter. Note that
// the returned name should be passed through t() for translation.
if ($op == 'list') {
return array(
0 => t('Substitute "Drupal"'),
1 => t('Substitute "mdwp*"'),
2 => t('Substitute [modulname/]'),
);
}
switch ($delta) {
// First we define the simple string substitution filter.
case 0:
switch ($op) {
// This description is shown in the administrative interface, unlike the
// filter tips which are shown in the content editing interface.
case 'description':
return t('Substitutes the string "Drupal" with a link to drupal.org.');
// We don't need the "prepare" operation for this filter, but it's required
// to at least return the input text as-is.
case 'prepare':
return $text;
// The actual filtering is performed here. The supplied text should be
// returned, once any necessary substitutions have taken place.
case 'process':
return str_replace('Drupal', '<a href="http://drupal.org">Drupal</a>', $text);
}
break;
case 1:
switch ($op) {
// This description is shown in the administrative interface, unlike the
// filter tips which are shown in the content editing interface.
case 'description':
return t('Substitutes the string "mdwp*" with a link to mdwp.de.');
case 'prepare':
return $text;
case 'process':
return str_replace('mdwp*', '<a href="http://mdwp.de">mdwp*</a>', $text);
}
break;
case 2:
switch ($op) {
// This description is shown in the administrative interface, unlike the
// filter tips which are shown in the content editing interface.
case 'description':
return t('Substitutes the string [modul#title] with a link to a project on drupal.org.');
case 'prepare':
return $text;
case 'process':
$text = str_replace('[', '<p class="download"><a href="http://drupal.org/project/', $text);
$text = str_replace('#', '">', $text);
$text = str_replace(']', '</a></p>', $text);
return $text;
}
break;
}
}
Anstelle von 3 Filtern hätte man das auch in einen schreiben können und die str_replace lassen sich sicherlich eleganter und effektiver mit preg_replace schreiben. Aber das tuts auch und da gefilterte Nodes gecached werden, spielt es für die Performance sicherlich keine große Rolle.
Neueste Kommentare
vor 6 Wochen 3 Tage
vor 6 Wochen 3 Tage
vor 6 Wochen 3 Tage
vor 6 Wochen 4 Tage
vor 18 Wochen 2 Tage
vor 21 Wochen 5 Tage
vor 22 Wochen 1 Stunde
vor 24 Wochen 4 Tage
vor 25 Wochen 4 Tage
vor 26 Wochen 6 Tage