Please check out the 2.x branch of Empty fields. I whipped it up mostly last night during my flight from dc to sf, heavily inspired by Boxes module. Looking for as much feedback as possible. I've defined the beginnings of the plugin system discussed in other threads, and implementing a new one is as easy as (from empty_fields_handler_text.inc):

/**
 * Defines EmptyFieldText.
 */
class EmptyFieldText extends EmptyFieldHandler {

  /**
   * Implementation of EmptyFieldText::defaults().
   *
   * @return array
   *   An array of default_values for the form below. Key names must match.
   */
  public function defaults() {
    return array(
      'empty_text' => '',
    );
  }

  /**
   * Implementation of EmptyFieldText::form().
   *
   * @return array
   *   A FAPI array to be used in configuration of this empty text plugin.
   */
  public function form(&$form_state = NULL) {
    $form['empty_text'] = array(
      '#type' => 'textarea',
      '#title' => t('Display Custom Text'),
      '#default_value' => isset($form_state['empty_text']) ? $form_state['empty_text'] : $this->options['empty_text'],
      '#description' => t('Display text if the field is empty.'),
    );

    return $form;
  }

  /**
   * Implementation of EmptyFieldText::react().
   *
   * @return string
   *   A rendered string of html for display.
   */
  public function react($context) {
    return filter_xss_admin(t('!empty_text', array('!empty_text' => $this->options['empty_text'])));
  }

  /**
   * Implementation of EmptyFieldText:summaryText().
   *
   * @return string
   *   Text for the field formatter settings summary.
   */
  public function summaryText() {
    return t('Empty Text: @empty_text', array('@empty_text' => $this->options['empty_text']));
  }
}

If you are interested in submitting plugins it would be great to get them rolled in. I've currently only had time to roll the default text version, but I expect that things like views embedding will be a nice touch. Also interested to see how you went about adding classes to your modules output.

Theres also an abstract method called alterOutput that will allow direct modification of the render array of the field before it is rendered, this would be good for plugins that need to do things to the label, etc.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Alan D.’s picture

Version: 7.x-1.x-dev » 7.x-2.x-dev
Status: Active » Needs work

Sorry, I have just gotten back to the project that is running with this.

Something in the last 3 months has broken the 1.x code, but I haven't investigated and have gone straight to 2.x to push this forward.

/**
 * Implements hook_field_formatter_info_alter().
 */
function empty_fields_field_formatter_info_alter(&$info) {
  foreach ($info as $formatter_key => &$formatter) {
    $formatter['settings']['empty_fields_handler'] = '';
    foreach (empty_fields_plugins() as $class_name => $title) {
      $plugin = new $class_name();
      foreach ($plugin->form() as $field_id => $default) {
        $field_name = empty_fields_generate_field_name($field_id, $class_name);
        $formatter['settings'][$field_name] = $default;
      }
    }
  }
}

Doesn't this mean to be $plugin->defaults()?

The declaration refers to the old name.

- * Implments hook_empty_fields_info().
+ * Implements hook_empty_fields().

And though it doesn't matter in PHP, there is a case mis-match in the class name in this hook.

Alan D.’s picture

OK, more.

From the top.

1) -11,10 +11,12
I have added a class_exists() in empty_fields_field_formatter_info_alter() as an error here prevents the registry flush from completing, even after errors in the code have been fixed.

2) -61,11 +63,11
Case / description change

3) -96,6 +98,9
Always return summary text

4) -118,6 +123
Most handlers probably do not have any settings.

5) -144,6 +149,13
Many entities were being processed multiple times. This tries to negate this.

6) -163,6 +175,11
We need context to the actual field that we are handling here!

7) -193,27 +210,30
Provides a way for a Handler to bale out if it decides that there is actually nothing to return

+      // Do not render empty values.
+      if (!empty($markup) || (is_scalar($markup) && drupal_strlen($markup))) {

Just realised that we are assuming a string here, $plugin->alterOutput(), is available if something else is required.

8) EmptyFieldHandler
Many description changes, but slight type hinting changes and concrete methods as only the two are required imho.

I got a fatal error on the live multi-site installation when deployed, so had to implement the quickest fix to normalise all of the method signatures. Looking at the patch, maybe type hints are better?

-  public function form(&$form_state = NULL) {
+  public function form(&$form_state = array()) {

to

-  public function form(&$form_state = NULL) {
+  public function form(array &$form_state) {

etc.

9) Readme
This was fully out of date

Alan D.’s picture

Alan D.’s picture

From the testing code review - using core drupal string functions.

From point from 7, changed code to always assume a scaler value.

RaulMuroc’s picture

Status: Needs work » Active

In this branch, when setting up display format, there is no choice for " Use field by default (if set) " as it was with the 7.x-1.x branch.

Alan D.’s picture

Status: Active » Needs work
rypit’s picture

Alan, Thanks so much for taking the time to review the 2.x branch. Hopefully you agree that this is a good way we can offer up an extensible way for people to provide empty field handlers. I'm totally on board with the changes you've purposed and I think we should roll them in. I'll try to get some testing in tonight and will report back here.
RJ

rypit’s picture

Raul, I'm not exactly sure I'm following your issue. The default behavior for the field are now called "Empty field is hidden". This is the way Drupal handles empty fields out of the box. Please create an issue with more details if that's not a satisfactory answer to your question.

Alan D.’s picture

I think that RaulMuroc may be having the same issue as I had with Better Formats or another module that saves an empty value

OR

The default value was set after content was created / the field was added after content was created.

Alan D.’s picture

And these are never used.

abstract class EmptyFieldHandler {
-  public $title;
-  public $description;
  public $options;
Alan D.’s picture

This patch includes the comment from #1 [EmptyFieldHandler::form() to EmptyFieldHandler::defaults()] and removes $form_state from EmptyFieldText::form(). This argument is not passed in and is used incorrectly and appears redundant afaick.

Whats up in empty_fields_load_plugin()? Did you mean this?

-    foreach ($plugin->defaults() as $field_id => $field) {
+    foreach ($plugin->defaults() as $field_id => $default) {
      $field_name = empty_fields_generate_field_name($field_id, $plugin_type);
      $plugin->options[$field_id] = !empty($view_mode_settings[$field_name]) ? $view_mode_settings[$field_name] : $default;
    }

[edit]
Let me know if the changes are OK and I'll push these. With the exception of the above, I think we are ready for a rc1 or full release on the 2.x branch. :)

rypit’s picture

Alan, that sounds good to me. Please do push those. I reviewed the earlier this week and they appeared to work fine. Thanks!

rypit’s picture

One thing to consider before a 2.0 release: lets make sure people using the empty text option from 1.x can seamlessly upgrade. I'll get to work on that ASAP.

rypit’s picture

Priority: Major » Normal
Status: Needs work » Active
Alan D.’s picture

Status: Active » Fixed

K, finally got back to this. Pushed it through with a couple coder review errors.

Changed API may require a notice

Alan D.’s picture

And a minor follow-up

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

add file path