Clarifications: This is about ChatGpt assisted creation of Drupal's 'Markdown Link Filter'
ChatGPT Direction
Folder/file structure
- folder structure: DrupalSiteRoot/modules/modules/custom/PrjctNm/src/Plugin/Filter
- files
- DrupalSiteRoot/modules/modules/custom/PrjctNm/mrk_dwn_lnk_fltr.info.yml
- module/project description file
name: 'mrk_dwn_lnk_fltr'
type: module
description: 'Converts Markdown links to wiki creation links for missing pages.'
core_version_requirement: ^8 || ^9 || ^10
package: Custom
dependencies:
- filter
- DrupalSiteRoot/modules/modules/custom/PrjctNm/src/Plugin/Filter/mrk_dwn_lnk_fltr.php
- the code/implementation
// simple worked version
<?phpnamespace Drupal\mrk_dwn_lnk_fltr\Plugin\Filter;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\filter\Annotation\Filter;/**
* @Filter(
* id = "mrk_dwn_lnk_fltr",
* title = @Translation("MarkDown Link Filter"),
* description = @Translation("Convert Markdown links to wiki creation links if the node does not exist."),
* type = Drupal\filter\Plugin\FilterInterface::TYPE_MARKUP_LANGUAGE,
* )
*/
class mrk_dwn_lnk_fltr extends FilterBase {/**
* {@inheritdoc}
*/
public function process($text, $langcode) {
$result = $text;// Regex to match Markdown links: [Text](Title)
$pattern = '/\[(.*?)\]\((.*?)\)/';\Drupal::logger('me')->debug('before user call');
$user = \Drupal::currentUser();
\Drupal::logger('me')->debug('after user call');
\Drupal::logger('me')->notice('Current user: uid=@uid name=@name roles=@roles', [
'@uid' => $user->id(),
'@name' => $user->getDisplayName(),
'@roles' => implode(', ', $user->getRoles()),
]);$content_type = 'wiki'; // adjust as needed
$result = 'user|'.$content_type.'|'.$result;
$result0 = preg_replace_callback($pattern, function($matches) use ($user, $content_type) {
$link_text = $matches[1];
$link_target = $matches[2];\Drupal::logger('me')->debug('CntntTyp='.$content_type);
\Drupal::logger('me')->debug('LnkTxt='.$link_text);
\Drupal::logger('me')->debug('LnkTrgtp='.$link_target);// Check if node with title exists
$query = \Drupal::entityTypeManager()->getStorage('node')->getQuery()
->accessCheck(FALSE) // do not check permission
->condition('title', $link_target) // query condition
->condition('type', $content_type) // :
->range(0, 1); // first 0 or 1 record
$nids = $query->execute(); // obtain node id info
\Drupal::logger('me')->debug('nids='.var_dump($nids));if (!empty($nids)) {
// Node exists, return normal link
return $matches[0];
} else {
// Node does not exist. Check if user can create
$node_access = \Drupal::entityTypeManager()->getStorage('node')->create(['type' => $content_type]);
if ($user->hasPermission('create ' . $content_type . ' content')) {
// Return Markdown link to node/add page + emoji
$create_url = '/node/add/' . $content_type . '?edit[title]=' . urlencode($link_target);
return '[' . $link_text . '](' . $create_url . ') ✨';
}
else {
// Node does not exist, user cannot create: just return normal text
return $link_text;
}
}
}, $text);
\Drupal::logger('me')->debug($result0);
return new FilterProcessResult($result0);
}
}
- see also C:\_DH\_Prsnl\_Cds\_ClPhp\_DrplRltd\WikiFltr\wikiFltr.php
- DrupalSiteRoot/modules/modules/custom/PrjctNm/mrk_dwn_lnk_fltr.info.yml
Enable module
- -Admin -Extend -List -custom -mrk_dwn_lnk_fltr = x
- -Install
Configure
- -Admin -Configuration -Content authoring -Text formats and editors -select format(e.g. Markdown)
- -Enabled filter -MarkDown Link Filter = x
- -set Filter processing order = move 'MarkDown Link Filter' to top
- Save configuration