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.
Comment | File | Size | Author |
---|---|---|---|
#16 | empty_fields-clean-up-follow-up-1714304-16.patch | 1.02 KB | Alan D. |
#2 | empty_fields-1714304-3-misc-changes.diff | 10.12 KB | Alan D. |
Comments
Comment #1
Alan D. CreditAttribution: Alan D. commentedSorry, 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.
Doesn't this mean to be
$plugin->defaults()
?The declaration refers to the old name.
And though it doesn't matter in PHP, there is a case mis-match in the class name in this hook.
Comment #2
Alan D. CreditAttribution: Alan D. commentedOK, 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
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?
to
etc.
9) Readme
This was fully out of date
Comment #3
Alan D. CreditAttribution: Alan D. commentedMarked #1797494: No options if the field lacks display formatter settings as duplicate
Comment #4
Alan D. CreditAttribution: Alan D. commentedFrom the testing code review - using core drupal string functions.
From point from 7, changed code to always assume a scaler value.
Comment #5
RaulMuroc CreditAttribution: RaulMuroc commentedIn 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.
Comment #6
Alan D. CreditAttribution: Alan D. commentedComment #7
rypit CreditAttribution: rypit commentedAlan, 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
Comment #8
rypit CreditAttribution: rypit commentedRaul, 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.
Comment #9
Alan D. CreditAttribution: Alan D. commentedI 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.
Comment #10
Alan D. CreditAttribution: Alan D. commentedAnd these are never used.
Comment #11
Alan D. CreditAttribution: Alan D. commentedThis 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?
[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. :)
Comment #12
rypit CreditAttribution: rypit commentedAlan, that sounds good to me. Please do push those. I reviewed the earlier this week and they appeared to work fine. Thanks!
Comment #13
rypit CreditAttribution: rypit commentedOne 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.
Comment #14
rypit CreditAttribution: rypit commentedComment #15
Alan D. CreditAttribution: Alan D. commentedK, finally got back to this. Pushed it through with a couple coder review errors.
Changed API may require a notice
Comment #16
Alan D. CreditAttribution: Alan D. commentedAnd a minor follow-up
Comment #17.0
(not verified) CreditAttribution: commentedadd file path