コード例 #1
0
ファイル: Configuration.php プロジェクト: aiesh/magento2
 /**
  * Accept option value and return its formatted view
  *
  * @param string|array $optionValue
  * Method works well with these $optionValue format:
  *      1. String
  *      2. Indexed array e.g. array(val1, val2, ...)
  *      3. Associative array, containing additional option info, including option value, e.g.
  *          array
  *          (
  *              [label] => ...,
  *              [value] => ...,
  *              [print_value] => ...,
  *              [option_id] => ...,
  *              [option_type] => ...,
  *              [custom_view] =>...,
  *          )
  * @param array $params
  * All keys are options. Following supported:
  *  - 'maxLength': truncate option value if needed, default: do not truncate
  *  - 'cutReplacer': replacer for cut off value part when option value exceeds maxLength
  *
  * @return array
  */
 public function getFormattedOptionValue($optionValue, $params = null)
 {
     // Init params
     if (!$params) {
         $params = array();
     }
     $maxLength = isset($params['max_length']) ? $params['max_length'] : null;
     $cutReplacer = isset($params['cut_replacer']) ? $params['cut_replacer'] : '...';
     // Proceed with option
     $optionInfo = array();
     // Define input data format
     if (is_array($optionValue)) {
         if (isset($optionValue['option_id'])) {
             $optionInfo = $optionValue;
             if (isset($optionInfo['value'])) {
                 $optionValue = $optionInfo['value'];
             }
         } else {
             if (isset($optionValue['value'])) {
                 $optionValue = $optionValue['value'];
             }
         }
     }
     // Render customized option view
     if (isset($optionInfo['custom_view']) && $optionInfo['custom_view']) {
         $_default = array('value' => $optionValue);
         if (isset($optionInfo['option_type'])) {
             try {
                 $group = $this->_productOptionFactory->create()->groupFactory($optionInfo['option_type']);
                 return array('value' => $group->getCustomizedView($optionInfo));
             } catch (\Exception $e) {
                 return $_default;
             }
         }
         return $_default;
     }
     // Truncate standard view
     if (is_array($optionValue)) {
         $truncatedValue = implode("\n", $optionValue);
         $truncatedValue = nl2br($truncatedValue);
         return array('value' => $truncatedValue);
     } else {
         if ($maxLength) {
             $truncatedValue = $this->filter->truncate($optionValue, array('length' => $maxLength, 'etc' => ''));
         } else {
             $truncatedValue = $optionValue;
         }
         $truncatedValue = nl2br($truncatedValue);
     }
     $result = array('value' => $truncatedValue);
     if ($maxLength && $this->string->strlen($optionValue) > $maxLength) {
         $result['value'] = $result['value'] . $cutReplacer;
         $optionValue = nl2br($optionValue);
         $result['full_view'] = $optionValue;
     }
     return $result;
 }
コード例 #2
0
ファイル: Data.php プロジェクト: kidaa30/magento2-platformsh
 /**
  * Return short detail info in HTML
  *
  * @param string $origDetail Full detail info
  * @return string
  */
 public function getDetailHtml($origDetail)
 {
     return nl2br($this->filter->truncate($this->_escaper->escapeHtml($origDetail), ['length' => 50]));
 }