Skip to content

Instantly share code, notes, and snippets.

@rudiedirkx
Last active December 20, 2015 15:59
Show Gist options
  • Save rudiedirkx/6158209 to your computer and use it in GitHub Desktop.
Save rudiedirkx/6158209 to your computer and use it in GitHub Desktop.
Saving a custom attribute into a link_field value.
.form-type-link-field {
display: -webkit-flex;
}
.form-item.form-type-link-field > :last-child {
-webkit-order: -1;
}
<?php
function test_element_info_alter(&$info) {
$info['link_field']['#process'][] = 'test_link_field_process';
unset($info['link_field']['#theme']);
}
function test_link_field_process($element) {
if ($element['#field_name'] == 'field_some_link') {
$element['attributes']['domain'] = array(
'#type' => 'select',
'#title' => 'Domain',
'#options' => drupal_map_assoc(array(1, 2, 7, 11, 12)),
);
if (isset($element['#default_value']['attributes']['domain'])) {
$element['attributes']['domain']['#default_value'] = $element['#default_value']['attributes']['domain'];
}
}
return $element;
}
function test_node_view_alter(&$build, $type) {
if (isset($build['field_some_link'])) {
$show = 7;
foreach ($build['field_some_link']['#items'] as $delta => $item) {
if (!empty($item['attributes']['domain']) && $item['attributes']['domain'] != $show) {
unset($build['field_some_link']['#items'][$delta]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment