0

I was planning to use an entity wrapper to conditionally fill in a (potentially) empty field. The use case is the following:

  • Some users want to manually fill in the field with machine name "field_citation".
  • Other users will not fill in the field.
  • If the field is empty, then I need to combine content from other fields to fill in the field_citation.

$Wrapper is a object created by the Entity API module and used to more easily manipulate content (clarification added due to request).

I thought the below code only would affect the view but apparently the save() function modify content in the database. So I need to learn how to properly display content in the field without permanently modifying it.

function theme_preprocess_html(&$vars) {

  if(isset($wrapper->field_citation)){  

    if(!$citation_content = $wrapper->field_citation->value()){
      $citation = implode($author_citation); //retrieved from #authorlisting
      $citation .= $wrapper->title->value() . '. ';
      $citation .= 'Website name. ';
      $citation .= date("Y/m/d", $wrapper->created->value());

      $wrapper->field_citation = $citation;
      $wrapper->save();


    }
  }    
}
2
  • I think it would be better if you would explain what exactly you are trying to do, in exact words, instead of expecting us to figure it from inexact description and incomplete code. And code sure is incomplete - $wrapper is not set before use, and it's not defined as global, so apparently you omitted significant part of code. Exact human readable algorithm, like "if field is empty, and if _______, then display _____ instead of field's content" would significantly improve chances for answer that meets what you are aiming for.
    – Mołot
    Commented Apr 10, 2014 at 14:14
  • $wrapper is an Entity metadata wrapper. I thought the first line of text and tags would be enough to clarify this but I am the one asking for help so obviously I am eager to clarify it further. Furthermore I described the issue in bullet points rather than free text to make it easier to see.
    – Sigvard
    Commented Apr 11, 2014 at 4:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.