Harmster Posted June 9, 2013 Share Posted June 9, 2013 Hey, I've made this really simple session module that lets to add messages to the session and render it. Here's the complete php file: <?php class SessionMessage extends Process implements Module, ConfigurableModule { public $messages = array(); public static function getModuleInfo() { return array( 'title' => 'SessionMessage', 'version' => 101, 'summary' => 'An example module used for demonstration purposes. See the /site/modules/Helloworld.module file for details.', 'href' => 'http://www.processwire.com', 'singular' => true, 'autoload' => true, ); } public function init() { $this->setFuel('sessionMessage', $this); } static public function getModuleConfigInputfields(array $data){ $modules = Wire::getFuel('modules'); $fields = new InputfieldWrapper(); $field = $modules->get("InputfieldText"); $field->attr('name+id', 'error_class'); $field->attr('value', $data['error_class']); $field->label = "Error class"; $field->description = 'Enter your error class for error handling.'; $fields->append($field); $field = $modules->get("InputfieldText"); $field->attr('name+id', 'success_class'); $field->attr('value', $data['success_class']); $field->label = "Success class"; $field->description = 'Enter your error class for success handling.'; $fields->append($field); $field = $modules->get("InputfieldText"); $field->attr('name+id', 'alert_class'); $field->attr('value', $data['alert_class']); $field->label = "Alert class"; $field->description = 'Enter your error class for alert handling.'; $fields->append($field); return $fields; } //void public function add($type, $message){ //types could be error, alert or success array_push($this->messages, array($type => $message)); $this->session->set('messages', $this->messages); } public function render($clean=true){ $html = ""; $this->messages = $this->session->get("messages"); foreach($this->messages as $array){ foreach($array as $type => $message){ $class = $type.'_class'; $html .= '<div class="' . $this->$class . '">'; $html .= $message; $html .'</div>'; } } $this->session->messages = $this->messages; if($clean == true){ unset($this->messages); $this->session->set('messages', $this->messages); } return $html; } } Installation: 1) Install the module 2) Give classes to each of the default fields: error success alert Usage: To add a message use: $sessionMessage->add('error', 'This is an error message'); To display all messages: echo $sessionMessage->render(); If you don't want the module to erase the messages after being displayed: echo $sessionMessage->render(false); I am currently using this in combination with Zurb Foundation Hope it helps someone. -Harmster 1 Link to comment Share on other sites More sharing options...
Wanze Posted June 9, 2013 Share Posted June 9, 2013 Harmster, Thanks for the module Just wanted to note that Pw has built in function for setting session messages: // Set message for next request $session->message('this message is accessible in the next request'); // or Error... $session->error('error message'); And check for messages could be done like this: if (count(wire('notices'))) { foreach (wire('notices') as $notice) { echo $notice->text; if ($notice instanceof NoticeError) echo "was error notice"; } } Edit: Just saw that you extend Process. This is only needed for Modules that execute a process in the admin of Pw. I think this is not the case for your module, so extending WireData would be enough. 1 Link to comment Share on other sites More sharing options...
OrganizedFellow Posted June 13, 2013 Share Posted June 13, 2013 I am currently using this in combination with Zurb Foundation Hope it helps someone. -Harmster I also use Foundation (currently the latest 4.2.1). Can you share an HTML snippet? I think I know how you are doing this, but I just wanna be sure. Link to comment Share on other sites More sharing options...
Yannick Albert Posted October 20, 2013 Share Posted October 20, 2013 Just for fun: https://gist.github.com/yckart/7067361 2 Link to comment Share on other sites More sharing options...
ryan Posted October 23, 2013 Share Posted October 23, 2013 Nice module you've put together there Yannick. Only thing I want to mention is that ProcessWire already has a $log API variable (see here) in the dev branch. The syntax of the PW $log API variable is very similar to yours, though it also supports writing to custom logs, but lacks the render() method that you have. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now