Backup code - ben600324/wiki GitHub Wiki
if ($form_id == 'node_landing_page_lb_edit_form') {
$form['actions']['submit']['#submit'][] = 'upload_files_via_url_submit';
// $remoteDocPath = 'https://www.orimi.com/pdf-test.pdf';
// $doc = system_retrieve_file($remoteDocPath, NULL, FALSE, FileSystemInterface::EXISTS_RENAME);
// $file = drupal_add_external_file($doc);
// $paragraph = Paragraph::create([
// 'title' => 'Parent paragraph',
// 'type' => 'document',
// 'field_document' => [
// 'target_id' => $file->id(),
// ],
// ]);
// $paragraph->save();
}
}
/**
* {@inheritdoc}
*/
function upload_files_via_url_submit(array &$form, FormStateInterface $form_state) {
// $remoteDocPath = 'https://www.orimi.com/pdf-test.pdf';
// $doc = system_retrieve_file($remoteDocPath, NULL, FALSE, FileSystemInterface::EXISTS_RENAME);
// $values = $form_state->getValues();
// dsm($values);
}
function drupal_add_external_file($file_drupal_path, $uid = 1, $status = 1) {
$files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => $file_drupal_path]);
$file = reset($files);
if (!$file) {
$file = File::create(['uri' => $file_drupal_path]);
$file->save();
}
return $file;
}
/**
* Implements hook_preprocess_file_link().
*/
function unhcr_preprocess_file_link($variables) {
// dsm($variables['link']);
$fieldParent = $variables['file']->_referringItem->getEntity();
$paragraph = \Drupal::entityTypeManager()
->getStorage('paragraph')
->load($fieldParent->id());
if ($paragraph->field_open_in_new_tab->value == 'yes') {
$options = $variables['link']['#url']->getOptions('attributes');
$options['attributes'] += [
'target' => '_blank',
];
$variables['link']['#url']->setOptions($options);
}
}
/**
* Implement hook_preprocess_field().
*/
function unhcr_preprocess_field(&$variables, $hook) {
if (!empty($variables['field_name'])) {
switch ($variables['field_name']) {
case 'field_document':
foreach ($variables['items'] as $key => $item) {
if (isset($item['content']['#paragraph']) && $item['content']['#paragraph']->field_file_icon->value == 'icone_style1') {
$item['attributes']->setAttribute('class', 'icone_style1');
}
if (isset($item['content']['#paragraph']) && $item['content']['#paragraph']->field_file_icon->value == 'icone_style2') {
$item['attributes']->setAttribute('class', 'icone_style2');
}
if (isset($item['content']['#paragraph']) && $item['content']['#paragraph']->field_file_icon->value == 'icone_style3') {
$item['attributes']->setAttribute('class', 'icone_style3');
}
}
break;
}
}
}
use Drupal\Core\Render\Markup; use Drupal\Core\Url; use Drupal\Core\Link;
// $options = ['absolute' => TRUE, 'attributes' => ['target' => '_blank']];
// $link_object = Link::createFromRoute($entity->getTitle(), 'entity.node.canonical', ['node' => $entity->id()], $options);
// Markup::create($link_object->toString())