Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Drupal 7 & 8 - useful field info on field management form: required, file uri_scheme, filefield_paths. Drupal 8 version in first comment
<?php
/**
* Implements hook_form_FORM_ID_alter().
*
* Indicate some details about the field on the field overview form
*/
function MYMODULE_form_field_ui_field_overview_form_alter(&$form, &$form_state, $form_id) {
$instances = field_info_instances( $form['#entity_type'], $form['#bundle'] );
$fields = field_info_fields();
foreach( $instances as $field_name => $instance ){
$field = $fields[ $field_name ];
if ( !empty( $instance['required'] ) ) {
$form['fields'][ $field_name ]['label']['#markup'].= " <small style='color: darkred;'>-required-</small>";
}
if ( !empty( $field['settings']['uri_scheme'] ) ){
$path = "{$field['settings']['uri_scheme']}://";
// filefield_paths module
if ( !empty( $instance['settings']['filefield_paths'] ) && $instance['settings']['filefield_paths_enabled'] ) {
if ( !empty( $instance['settings']['filefield_paths']['file_path']['value'] ) ){
$path.= "{$instance['settings']['filefield_paths']['file_path']['value']}/";
}
$path.= "{$instance['settings']['filefield_paths']['file_name']['value']}";
if ( $instance['settings']['filefield_paths']['active_updating'] ) {
$path.= "<br>-active updating-";
}
}
// core drupal
else {
$path.= "{$instance['settings']['file_directory']}/";
}
$form['fields'][ $field_name ]['label']['#markup'].= "<br><small style='color: darkred;'>{$path}</small>";
}
}
}
@daggerhart
Copy link
Author

daggerhart commented Aug 22, 2017

For Drupal 8 you need to take over the FieldConfigListBuilder:

mymodule/src/MyModuleFieldConfigListBuilder.php

<?php

namespace Drupal\mymodule;
use Drupal\Core\Entity\EntityInterface;
use Drupal\field_ui\FieldConfigListBuilder;

class MyModuleFieldConfigListBuilder extends FieldConfigListBuilder {

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $field_config) {
    $row = parent::buildRow($field_config);
    $details = [];

    if ($field_config->isRequired()) {
      $details[] = "-required-";
    }

    if ($field_config->getType() == 'file') {
      $settings = $field_config->getSettings();
      $ffp = $field_config->getThirdPartySettings('filefield_paths');
      $path = "{$settings['uri_scheme']}://{$settings['file_directory']}";

      if (!empty($ffp)) {
        if ($ffp['enabled']) {
          $path = "{$settings['uri_scheme']}://{$ffp['file_path']['value']}/{$ffp['file_name']['value']}";

          if ($ffp['active_updating']) {
            $details[] = '-active update-';
          }
        }
      }

      $details[] = $path;
    }

    if (!empty($details)) {
      $row['data']['label'] = [
        'data' => [
          '#type' => 'container',
          'label' => [
            '#markup' => $field_config->label()
          ],
          'details' => [
            '#markup' => "<br><small class='color-error'>".implode('<br>', $details)."</small>"
          ]
        ]
      ];
    }

    return $row;
  }
}

mymodule/mymodule.module

<?php

/**
 * Implements hook_entity_type_alter()
 *
 * @param array $entity_types
 */
function mymodule_entity_type_alter(array &$entity_types) {
  $entity_types['field_config']->setListBuilderClass('Drupal\mymodule\MyModuleFieldConfigListBuilder');
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment