setData() public method

This method allows storage of arbitrary data with this menu item. The data can be used for sorting, custom rendering, or any other use.
public setData ( mixed $key, mixed $value = null ) : void
$key mixed String key or an associative array of key/value pairs
$value mixed The value if $key is a string
return void
Example #1
0
 public function testSetDataWithArray()
 {
     $item = new \ElggMenuItem('name', 'text', 'url');
     $item->setData(array('priority' => 88, 'new' => 64));
     $this->assertEquals(88, $item->getData('priority'));
     $this->assertEquals(88, $item->getPriority());
     $this->assertEquals(64, $item->getData('new'));
 }
Example #2
0
 /**
  * ElggMenuItem factory method
  *
  * This static method creates an ElggMenuItem from an associative array.
  * Required keys are name, text, and href.
  *
  * @param array $options Option array of key value pairs
  *
  * @return ElggMenuItem or NULL on error
  */
 public static function factory($options)
 {
     if (!isset($options['name']) || !isset($options['text'])) {
         return NULL;
     }
     if (!isset($options['href'])) {
         $options['href'] = '';
     }
     $item = new ElggMenuItem($options['name'], $options['text'], $options['href']);
     unset($options['name']);
     unset($options['text']);
     unset($options['href']);
     // special catch in case someone uses context rather than contexts
     if (isset($options['context'])) {
         $options['contexts'] = $options['context'];
         unset($options['context']);
     }
     // make sure contexts is set correctly
     if (isset($options['contexts'])) {
         $item->setContext($options['contexts']);
         unset($options['contexts']);
     }
     if (isset($options['link_class'])) {
         $item->setLinkClass($options['link_class']);
         unset($options['link_class']);
     }
     if (isset($options['item_class'])) {
         $item->setItemClass($options['item_class']);
         unset($options['item_class']);
     }
     if (isset($options['data']) && is_array($options['data'])) {
         $item->setData($options['data']);
         unset($options['data']);
     }
     foreach ($options as $key => $value) {
         if (isset($item->data[$key])) {
             $item->data[$key] = $value;
         } else {
             $item->{$key} = $value;
         }
     }
     return $item;
 }
 /**
  * ElggMenuItem factory method
  *
  * This static method creates an ElggMenuItem from an associative array.
  * Required keys are name, text, and href.
  *
  * @param array $options Option array of key value pairs
  *
  * @return ElggMenuItem or null on error
  */
 public static function factory($options)
 {
     if (!isset($options['name']) || !isset($options['text'])) {
         return null;
     }
     if (!isset($options['href'])) {
         $options['href'] = '';
     }
     $item = new ElggMenuItem($options['name'], $options['text'], $options['href']);
     unset($options['name']);
     unset($options['text']);
     unset($options['href']);
     // special catch in case someone uses context rather than contexts
     if (isset($options['context'])) {
         $options['contexts'] = $options['context'];
         unset($options['context']);
     }
     // make sure contexts is set correctly
     if (isset($options['contexts'])) {
         $item->setContext($options['contexts']);
         unset($options['contexts']);
     }
     if (isset($options['link_class'])) {
         $item->setLinkClass($options['link_class']);
         unset($options['link_class']);
     } elseif (isset($options['class'])) {
         elgg_deprecated_notice("ElggMenuItem::factory() does not accept 'class' key anymore, use 'link_class' instead", 1.9);
         $item->setLinkClass($options['class']);
         unset($options['class']);
     }
     if (isset($options['item_class'])) {
         $item->setItemClass($options['item_class']);
         unset($options['item_class']);
     }
     if (isset($options['data']) && is_array($options['data'])) {
         $item->setData($options['data']);
         unset($options['data']);
     }
     foreach ($options as $key => $value) {
         if (isset($item->data[$key])) {
             $item->data[$key] = $value;
         } else {
             $item->{$key} = $value;
         }
     }
     return $item;
 }